[英]apply(list) to multiple columns in pandas
I currently have a dataframe
that looks like this :我目前有一个看起来像这样的
dataframe
:
df = pd.DataFrame({'A': [1,1,2,2,2,2,3],
'B':['a','b','c','d','e','f','g'],
'C':['1','2','3','4','5','6','7']})
df2 = df.groupby('A')['B'].apply(list).reset_index()
produces this : df2 = df.groupby('A')['B'].apply(list).reset_index()
产生这个:
A B
0 1 [a, b]
1 2 [c, d, e, f]
2 3 [g]
How can I produce this?我怎样才能生产这个?
A B C
0 1 [a, b] [1, 2]
1 2 [c, d, e, f] [3, 4, 5, 6]
2 3 [g] [7]
You can do it like this你可以这样做
df.groupby('A', as_index=False).agg(B=("B", list), C=("C", list))
A B C
0 1 [a, b] [1, 2]
1 2 [c, d, e, f] [3, 4, 5, 6]
2 3 [g] [7]
Or equivalently或等效地
pd.pivot_table(data=df, index="A", values=["B", "C"], aggfunc=list).reset_index()
Use:利用:
df.groupby('A', as_index=False).agg(list)
This gives you:这给了你:
A B C
0 1 [a, b] [1, 2]
1 2 [c, d, e, f] [3, 4, 5, 6]
2 3 [g] [7]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.