pandas 将列添加到 groupby 数据框 [英] pandas add column to groupby dataframe
本文介绍了 pandas 将列添加到 groupby 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个简单的数据框df
:
I have this simple dataframe df
:
df = pd.DataFrame({'c':[1,1,1,2,2,2,2],'type':['m','n','o','m','m','n','n']})
我的目标是为每个c
计算type
的值,然后添加一个c
大小的列.所以开始:
my goal is to count values of type
for each c
, and then add a column with the size of c
. So starting with:
In [27]: g = df.groupby('c')['type'].value_counts().reset_index(name='t')
In [28]: g
Out[28]:
c type t
0 1 m 1
1 1 n 1
2 1 o 1
3 2 m 2
4 2 n 2
第一个问题解决了.那我也可以:
the first problem is solved. Then I can also:
In [29]: a = df.groupby('c').size().reset_index(name='size')
In [30]: a
Out[30]:
c size
0 1 3
1 2 4
如何将 size
列直接添加到第一个数据帧?到目前为止,我使用 map
作为:
How can I add the size
column directly to the first dataframe? So far I used map
as:
In [31]: a.index = a['c']
In [32]: g['size'] = g['c'].map(a['size'])
In [33]: g
Out[33]:
c type t size
0 1 m 1 3
1 1 n 1 3
2 1 o 1 3
3 2 m 2 4
4 2 n 2 4
哪个有效,但有没有更直接的方法来做到这一点?
which works, but is there a more straightforward way to do this?
推荐答案
使用 transform
将一列从 groupby
聚合添加回原始 df,transform
返回一个 Series
其索引与 orig df 对齐:
Use transform
to add a column back to the orig df from a groupby
aggregation, transform
returns a Series
with its index aligned to the orig df:
In [123]:
g = df.groupby('c')['type'].value_counts().reset_index(name='t')
g['size'] = df.groupby('c')['type'].transform('size')
g
Out[123]:
c type t size
0 1 m 1 3
1 1 n 1 3
2 1 o 1 3
3 2 m 2 4
4 2 n 2 4
这篇关于 pandas 将列添加到 groupby 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文