pandas 在多列熊猫上应用“列表”功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40357671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:19:44  来源:igfitidea点击:

Apply "list" function on multiple columns pandas

pythonlistpandasapply

提问by matlabit

In order to "concatenate" a few rows to 1 list with groupby in Pandas, I can do this:

为了在 Pandas 中使用 groupby 将几行“连接”到 1 个列表,我可以这样做:

df = pd.DataFrame({'A': [1,1,2,2,2,2,3],'B':['a','b','c','d','e','f','g']})

df = df.groupby('A')['B'].apply(list)

I will get:

我会得到:

A
-------------------
1          [a, b]
2    [c, d, e, f]
3             [g]

I want to do the same with agg:

我想对 agg 做同样的事情:

f = {"B":[list]}
df = df.groupby('A').agg(f)

that gives errors, any idea?

这给出了错误,知道吗?

Thanks,

谢谢,

回答by jezrael

You can use tolist- output is Series:

您可以使用tolist- 输出是Series

df = df.groupby('A')['B'].agg(lambda x: x.tolist())
print (df)
A
1          [a, b]
2    [c, d, e, f]
3             [g]
dtype: object

Or with define column Bin dict- output is DataFrame:

或者使用定义列B输入dict- 输出是DataFrame

df = df.groupby('A').agg({'B': lambda x: x.tolist()})
print (df)
              B
A              
1        [a, b]
2  [c, d, e, f]
3           [g]

Also works:

也有效:

df = df.groupby('A')['B'].agg(lambda x: list(x))
print (df)
A
1          [a, b]
2    [c, d, e, f]
3             [g]
dtype: object

df = df.groupby('A').agg({'B': lambda x: list(x)})
print (df)
              B
A              
1        [a, b]
2  [c, d, e, f]
3           [g]