[英]Group by the all the columns except the first one, but aggregate as list the first column
Let's say, I have this dataframe:比方说,我有这个 dataframe:
df = pd.DataFrame({'col_1': ['yes','no'], 'test_1':['a','b'], 'test_2':['a','b']})
What I want, is to group by all the columns except the first one and aggregate the results where the group by is the same.我想要的是对除第一列以外的所有列进行分组,并汇总分组依据相同的结果。
This is what I'm trying:这就是我正在尝试的:
col_names = df.columns.to_list()
df_out = df.groupby([col_names[1:]])[col_names[0]].agg(list)
This is my end data frame goal:这是我的最终数据框目标:
df = pd.DataFrame({'col_1': [['yes','no']], 'test_1':['a'], 'test_2':['b']})
And, if I have more rows, I want it to behave with the same principle, join in list the groups that are the same based on the column [1:] (from the second till end.而且,如果我有更多行,我希望它以相同的原则运行,加入基于列 [1:] 的相同组列表(从第二个到最后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.