组合假人并计算熊猫数据框架

发布于 2025-02-05 05:54:27 字数 1617 浏览 2 评论 0原文

我有这样的pandas dataframe:

“原始表”

作为纯文本:

{'id; sub_id; value; total_stuff与ID和sub_id': ['aaa; 1; cat; cat; 10','aaa; 1; cat; cat; 10','aaa; 1; dog; dog; 10','aaa; 2; cat; cat; 7', 'aaa; 2;狗; 7','aaa; 3; cat; 5','bbb; 1; panda; 20','bbb; 1; cat; cat; 20', 'bbb; 2; panda; 12']}

我想要的所需输出就是这样。

请注意,可能存在许多不同的“值”,因此我需要自动化虚拟变量的创建(NB_Animals)。 但是这些假人变量必须包含ID和sub_id的事件数量。 对于给定的ID/SUB_ID组合,Total_Stuff始终是相同的值。

我已经尝试使用get_dummies(df,columns = ['value']),这给了我这个表。

使用get_dummies

作为纯文本:

{'id; sub_id; value_cat; value_dog; value_panda; total_stuff与id相关 and sub_id':['aaA; 1; 1; 1; 0; 0; 10','aaA; 1; 1; 1; 1; 0; 0; 10','aaa; 1; 1; 1; 1; 0; 0; 10',10',, 'aaA; 2; 1; 1; 0; 7','aaA; 2; 1; 1; 1; 0; 7','aaA; 3; 1; 0; 0; 0; 0; 5',','bbb; 1; 1; 1; 0; 0 ; 1; 20',, 'bbb; 1; 1; 0; 1; 20','bbb; 2; 0; 0; 1; 1; 12']}

我很想使用某种df.groupby(['id',', 'sub_id'])。agg({'value_cat':'sum','value_dog':'sum',...,'total_stuff':'mean'}),但要写所有可能的动物价值太乏味了。

因此,如何获得值适当的汇总计数/值的值,以及total_stuff的平均值(由于total_stuff为唯一/id/sub_id组合),

谢谢

编辑:感谢Chikich的整洁答案。 agg_dict是我需要的

I have a pandas dataframe like this:

original table

as a plain text:

{'id;sub_id;value;total_stuff related to id and sub_id':
['aaa;1;cat;10', 'aaa;1;cat;10', 'aaa;1;dog;10', 'aaa;2;cat;7',
'aaa;2;dog;7', 'aaa;3;cat;5', 'bbb;1;panda;20', 'bbb;1;cat;20',
'bbb;2;panda;12']}

The desired output I want is this.

desired output

Note that there are many different "values" possible, so I would need to automate the creation of dummies variables (nb_animals).
But these dummies variables must contain the number of occurences by id and sub_id.
The total_stuff is always the same value for a given id/sub_id combination.

I've tried using get_dummies(df, columns = ['value']), which gave me this table.

using get_dummies

as a plain text:

{'id;sub_id;value_cat;value_dog;value_panda;total_stuff related to id
and sub_id': ['aaa;1;2;1;0;10', 'aaa;1;2;1;0;10', 'aaa;1;2;1;0;10',
'aaa;2;1;1;0;7', 'aaa;2;1;1;0;7', 'aaa;3;1;0;0;5', 'bbb;1;1;0;1;20',
'bbb;1;1;0;1;20', 'bbb;2;0;0;1;12']}

I'd love to use some kind of df.groupby(['id','sub_id']).agg({'value_cat':'sum', 'value_dog':'sum', ... , 'total_stuff':'mean'}), but writing all of the possible animal values would be too tedious.

So how to get a proper aggregated count/sum for values, and average for total_stuff (since total_stuff is unique per id/sub_id combination)

Thanks

EDIT : Thanks chikich for the neat answer. The agg_dict is what I needed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人哭 2025-02-12 05:54:27

使用pd.get_dummies用于转换分类数据

df = pd.get_dummies(df, prefix='nb', columns='value')

,然后由ID组和SubID组

agg_dict = {key: 'sum' for key in df.columns if key[:3] == 'nb_'}
agg_dict['total_stuff'] = 'mean'
df = df.groupby(['id', 'subid']).agg(agg_dict).reset_index()

Use pd.get_dummies to transform categorical data

df = pd.get_dummies(df, prefix='nb', columns='value')

Then group by id and subid

agg_dict = {key: 'sum' for key in df.columns if key[:3] == 'nb_'}
agg_dict['total_stuff'] = 'mean'
df = df.groupby(['id', 'subid']).agg(agg_dict).reset_index()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文