将列值分组在一起 [英] Grouping column values together
问题描述
我有一个像这样的数据框:
I have a dataframe like so:
Class price demand
1 22 8
1 60 7
3 32 14
2 72 9
4 45 20
5 42 25
我想做的是将 1-3 班归为一类,将 4-5 班归为一类.然后我想得到每个类别的价格总和和每个类别的需求总和.我也想得到平均值.结果应该是这样的:
What I'd like to do is group classes 1-3 in one category and classes 4-5 in one category. Then I'd like to get the sum of price for each category and the sum of demand for each category. I'd like to also get the mean. The result should look something like this:
Class TotalPrice TotalDemand AveragePrice AverageDemand
P 186 38 46.5 9.5
E 87 45 43.5 22.5
其中 P 是 1-3 级,E 是 4-5 级.如何在熊猫中按类别分组?有没有办法做到这一点?
Where P is classes 1-3 and E is classes 4-5. How can I group by categories in pandas? Is there a way to do this?
推荐答案
您可以创建一个字典来定义您的组.
You can create a dictionary that defines your groups.
mapping = {**dict.fromkeys([1, 2, 3], 'P'), **dict.fromkeys([4, 5], 'E')}
然后,如果您将字典或可调用对象传递给 groupby
,它会自动映射到索引上.所以,让我们将索引设置为 Class
Then if you pass a dictionary or callable to a groupby
it automatically gets mapped onto the index. So, let's set the index to Class
d = df.set_index('Class').groupby(mapping).agg(['sum', 'mean']).sort_index(1, 1)
最后,我们进行了一些调整,以按照您指定的方式获取列名.
Finally, we do some tweaking to get column names the way you specified.
rename_dict = {'sum': 'Total', 'mean': 'Average'}
d.columns = d.columns.map(lambda c: f"{rename_dict[c[1]]}{c[0].title()}")
d.rename_axis('Class').reset_index()
Class TotalPrice TotalDemand AveragePrice AverageDemand
0 E 87 45 43.5 22.5
1 P 186 38 46.5 9.5
这篇关于将列值分组在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!