[英]pandas add column to groupby dataframe
I have this simple dataframe df
:我有这个简单的数据框df
:
df = pd.DataFrame({'c':[1,1,1,2,2,2,2],'type':['m','n','o','m','m','n','n']})
my goal is to count values of type
for each c
, and then add a column with the size of c
.我的目标是计算每个c
的type
值,然后添加一个大小为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
How can I add the size
column directly to the first dataframe?如何将size
列直接添加到第一个数据框? So far I used map
as:到目前为止,我使用map
作为:
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?哪个有效,但是有没有更直接的方法来做到这一点?
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:使用transform
将一列从groupby
聚合中添加回原点 df, transform
返回一个Series
,其索引与原点 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
Another solution with transform
len
: transform
len
另一个解决方案:
df['size'] = df.groupby('c')['type'].transform(len)
print df
c type size
0 1 m 3
1 1 n 3
2 1 o 3
3 2 m 4
4 2 m 4
5 2 n 4
6 2 n 4
Another solution with Series.map
and Series.value_counts
: Series.map
和Series.value_counts
另一个解决方案:
df['size'] = df['c'].map(df['c'].value_counts())
print (df)
c type size
0 1 m 3
1 1 n 3
2 1 o 3
3 2 m 4
4 2 m 4
5 2 n 4
6 2 n 4
You can calculate the groupby object and use it multiple times:您可以计算 groupby 对象并多次使用它:
g = df.groupby('c')['type']
g.value_counts().reset_index(name='counts').assign(size=g.transform('size'))
Output:输出:
c type counts 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.