连接具有相同 ID 的 Pandas DataFrame 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34776651/
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 00:30:23  来源:igfitidea点击:

Concatenate rows of pandas DataFrame with same id

pythonpandasdataframe

提问by Alex

Say I have a pandas DataFrame such as:

假设我有一个 Pandas DataFrame,例如:

   A  B  id
0  1  1   0
1  2  1   0
2  3  2   1
3  0  2   1

Say I want to combine rows with the same id so that the other elements in the rows get put together in a list, so that the above dataframe would become:

假设我想组合具有相同 id 的行,以便行中的其他元素放在一个列表中,这样上面的数据框就会变成:

     A       B     id
0  [1, 2]  [1, 1]   0
1  [3, 0]  [2, 2]   1

as the first two rows, and the last two rows have the same id. Does pandas have a function to do this? I am aware of the pandas groupby command, but I would like the return type to be a dataframe as well. Thanks.

作为前两行,后两行具有相同的 id。大Pandas有这样做的功能吗?我知道 pandas groupby 命令,但我希望返回类型也为数据帧。谢谢。

回答by Anton Protopopov

You could use groupbyfor that with groupby aggmethod and tolistmethod of Pandas Series:

您可以使用groupbygroupbyagg方法和tolistPandas 系列方法:

In [762]: df.groupby('id').agg(lambda x: x.tolist())
Out[762]: 
         A       B
id                
0   [1, 2]  [1, 1]
1   [3, 0]  [2, 2]

groupbyreturn an Dataframe as you want:

groupby根据需要返回数据框:

In [763]: df1 = df.groupby('id').agg(lambda x: x.tolist())

In [764]: type(df1)
Out[764]: pandas.core.frame.DataFrame

To exactly match your expected result you could additionally do reset_indexor use as_index=Falsein groupby:

要完全匹配您的预期结果,您还可以在以下内容中执行reset_index或使用:as_index=Falsegroupby

In [768]: df.groupby('id', as_index=False).agg(lambda x: x.tolist())
Out[768]: 
   id       A       B
0   0  [1, 2]  [1, 1]
1   1  [3, 0]  [2, 2]

In [771]: df1.reset_index()
Out[771]: 
   id       A       B
0   0  [1, 2]  [1, 1]
1   1  [3, 0]  [2, 2]