如何考虑平均值和列总和上的熊猫数据框架?
我有以下dataframe pandas:
import pandas as pd
df = pd.DataFrame({'ID': ['1a','1a','3a','4a','3a'],
'column_1': [10.6, 10.4, 10, 20, 20],
'column_2': [10, 20, 30, 40, 50]
})
print(df)
ID column_1 column_2
1a 10.6 10
1a 10.4 20
3a 10.0 30
4a 20.0 40
3a 20.0 50
我需要进行分组so so“ id”。因此,我这样做了:
df_result = df.groupby(['ID'])
现在,我想按平均列_1的平均值和“ column_2”的总和进行分组。但是我只能进行平均操作:
df_result.mean()
输出:
column_1 column_2
ID
1a 10.5 15
3a 15.0 40
4a 20.0 40
所需输出:
ID column_1 column_2
1a 10.5 30
3a 15.0 80
4a 20.0 30
I have the following dataframe pandas:
import pandas as pd
df = pd.DataFrame({'ID': ['1a','1a','3a','4a','3a'],
'column_1': [10.6, 10.4, 10, 20, 20],
'column_2': [10, 20, 30, 40, 50]
})
print(df)
ID column_1 column_2
1a 10.6 10
1a 10.4 20
3a 10.0 30
4a 20.0 40
3a 20.0 50
I need to do a grouping so 'ID'. So I did this:
df_result = df.groupby(['ID'])
Now, I would like to group by the average of the column_1 and the sum of the 'column_2'. But I was only able to do the average operation:
df_result.mean()
Output:
column_1 column_2
ID
1a 10.5 15
3a 15.0 40
4a 20.0 40
desired output:
ID column_1 column_2
1a 10.5 30
3a 15.0 80
4a 20.0 30
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论