pandas 崩溃的行数据 [英] pandas collapsing row data
本文介绍了 pandas 崩溃的行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大熊猫新手,我在这里学习. 我有一个看起来像这样的数据框:
New to pandas, I m climbing my learning curve here. I have a dataframe that looks like:
Relinquished Degree Count DTD DNP outindefinitely seasonout
-----------------------------------------------------------
player1 1 1 1
player1 3 1 1
程度"列中的值在1到4的范围内,它表示伤害程度(DTD,DNP,无限期,反季节). 如何在已放弃"列下合并具有相同玩家名称的两行? 预期输出:
Values in column Degree ranges from 1 to 4 and it represents degree of injury (DTD,DNP,outindefinitely,seasonout). How do I merge the two rows that has same player name under column Relinquished? Expected output:
Relinquished DTD DNP outindefinitely seasonout
-----------------------------------------------------------
player1 1 1
提前谢谢!
推荐答案
您不能在将缺少的值替换为空字符串第一步,然后汇总sum
:
You cannot replace missing values to empty strings in first step and then aggregate sum
:
df = df.groupby('Relinquished').sum()
如果要指定总和列:
print (df)
Relinquished Degree Count DTD DNP outindefinitely seasonout
0 player1 1 1 1.0 NaN NaN NaN
1 player1 3 1 NaN NaN 1.0 NaN
df = df.groupby('Relinquished')['DTD','DNP','outindefinitely','seasonout'].sum()
print (df)
DTD DNP outindefinitely seasonout
Relinquished
player1 1.0 0.0 1.0 0.0
这篇关于 pandas 崩溃的行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文