连接具有相同 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
Concatenate rows of pandas DataFrame with same id
提问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 groupby
for that with groupby agg
method and tolist
method of Pandas Series:
您可以使用groupby
groupbyagg
方法和tolist
Pandas 系列方法:
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]
groupby
return 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_index
or use as_index=False
in groupby
:
要完全匹配您的预期结果,您还可以在以下内容中执行reset_index
或使用:as_index=False
groupby
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]