pandas 中的新列-通过应用列表groupby将系列添加到数据框 [英] New column in pandas - adding series to dataframe by applying a list groupby
本文介绍了 pandas 中的新列-通过应用列表groupby将系列添加到数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
提供以下df
Id other concat
0 A z 1
1 A y 2
2 B x 3
3 B w 4
4 B v 5
5 B u 6
我想要带有分组值作为列表的new
列的结果
I want the result with new
column with grouped values as list
Id other concat new
0 A z 1 [1, 2]
1 A y 2 [1, 2]
2 B x 3 [3, 4, 5, 6]
3 B w 4 [3, 4, 5, 6]
4 B v 5 [3, 4, 5, 6]
5 B u 6 [3, 4, 5, 6]
这类似于以下问题:
复制GROUP_CONCAT到pandas.DataFrame
但是,它将从df.groupby('Id')['concat'].apply(list)
(它是一个小于数据框的大小的Series
)获得的分组应用于原始数据框.
However, it is apply the grouping you get from df.groupby('Id')['concat'].apply(list)
, which is a Series
of smaller size than the dataframe, to the original dataframe.
我尝试了下面的代码,但未将其应用于数据框:
I have tried the code below, but it does not apply this to the dataframe:
import pandas as pd
df = pd.DataFrame( {'Id':['A','A','B','B','B','C'], 'other':['z','y','x','w','v','u'], 'concat':[1,2,5,5,4,6]})
df.groupby('Id')['concat'].apply(list)
我知道transform
可用于将分组应用于数据框,但是在这种情况下不起作用.
I know that transform
can be used to apply groupings to dataframes, but it does not work in this case.
>>> df['new_col'] = df.groupby('Id')['concat'].transform(list)
>>> df
Id concat other new_col
0 A 1 z 1
1 A 2 y 2
2 B 5 x 5
3 B 5 w 5
4 B 4 v 4
5 C 6 u 6
>>> df['new_col'] = df.groupby('Id')['concat'].apply(list)
>>> df
Id concat other new_col
0 A 1 z NaN
1 A 2 y NaN
2 B 5 x NaN
3 B 5 w NaN
4 B 4 v NaN
5 C 6 u NaN
推荐答案
groupby
与join
df.join(df.groupby('Id').concat.apply(list).to_frame('new'), on='Id')
这篇关于 pandas 中的新列-通过应用列表groupby将系列添加到数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文