有以下数据:
board_href_deals items test1
0 test2 {'x': 'a'} test1
1 test2 {'x': 'b'} test2
对“board_href_deals”进行分组后, 我想以列表格式输出现有数据,如下所示:
board_href_deals items test1
0 test2 [{'x': 'a'}, {'x': 'b'}] ['test1', 'test2']
谢谢
最佳答案
使用DataFrameGroupBy.agg
,在 pandas 0.23.4
中测试:
df = df.groupby('board_href_deals', as_index=False).agg(list)
print (df)
board_href_deals items test1
0 test2 [{'x': 'a'}, {'x': 'b'}] [test1, test2]
谢谢@jpp 为老 Pandas 提供的解决方案:
df = df.groupby('board_href_deals').agg(lambda x: list(x))
关于python - pandas 将相同的值合并在同一行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52075442/