串联具有相同ID的Pandas DataFrame行 [英] Concatenate rows of pandas DataFrame with same id
问题描述
说我有一个熊猫DataFrame,例如:
Say I have a pandas DataFrame such as:
A B id
0 1 1 0
1 2 1 0
2 3 2 1
3 0 2 1
说我想合并具有相同id的行,以便将行中的其他元素放到一个列表中,这样上面的数据框将变为:
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:
A B id
0 [1, 2] [1, 1] 0
1 [3, 0] [2, 2] 1
作为前两行,后两行具有相同的ID.熊猫有功能吗?我知道pandas groupby命令,但是我也希望返回类型也是一个数据框.谢谢.
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.
推荐答案
您可以使用 tolist
方法:
You could use groupby
for that with groupby agg
method and tolist
method of Pandas Series:
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
根据需要返回一个数据框:
groupby
return an Dataframe as you want:
In [763]: df1 = df.groupby('id').agg(lambda x: x.tolist())
In [764]: type(df1)
Out[764]: pandas.core.frame.DataFrame
要完全符合您的预期结果,您可以另外执行reset_index
或在groupby
中使用as_index=False
:
To exactly match your expected result you could additionally do reset_index
or use as_index=False
in 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]
这篇关于串联具有相同ID的Pandas DataFrame行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!