[英]Using GroupBy to add new columns to data frame
I have a data frame like so 我有一个像这样的数据框架
id val1 val2
0 A B
1 C D
1 E F
2 G H
and trying to reshape into... 并试图重塑成......
id val1 val2 val3 val4
0 A B
1 C D E F
2 G H
It doesn't matter what the additional column names are and I may not know how many duplicates there are of each id, so I may not know exactly how many columns to add. 附加列名称是什么并不重要,我可能不知道每个id有多少重复,所以我可能不知道要添加多少列。
Any advice for solving a problem like this? 有什么建议可以解决这样的问题吗? I've been trying to use pandas and groupBy, but I'm not constrained to either.
我一直在尝试使用pandas和groupBy,但我也不会受到限制。 Thanks!
谢谢!
This is a pivot
problem, but you'll need to convert your frame from wide to long before you can pivot it: 这是一个
pivot
问题,但您需要将框架从宽到长转换为可以转动它:
u = df.melt('id')
u.assign(variable=u.groupby('id').cumcount()).pivot(*u)
variable 0 1 2 3
id
0 A B NaN NaN
1 C E D F
2 G H NaN NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.