[英]Pandas how to pivot/unpivot/add a dummy column name
I want to convert from a long to a wide table with dummy column names created based on the number of accid我想从长表转换为宽表,其中包含基于 accid 数创建的虚拟列名
sample excel input vs output attached样品 excel 输入与 output 附加
Please help请帮忙
I was able to get down to 2 steps, pivot_table
using aggfunc=list
, and then creating new columns from that list.我能够减少到 2 个步骤,使用
pivot_table
aggfunc=list
进行数据透视表,然后从该列表创建新列。
I'm not sure I've come up with what you want though, because the assignment to columns is just filling up from the left.我不确定我是否想出了你想要的,因为对列的分配只是从左边填满。
Create the DataFrame:创建 DataFrame:
so = pd.DataFrame({'AccID': 'B1 B2 B3 B4 B5 B6 B7'.split(),
'UserID': 'A1 A1 A1 A2 A2 A3 A4'.split()}
)
AccID UserID
0 B1 A1
1 B2 A1
2 B3 A1
3 B4 A2
4 B5 A2
5 B6 A3
6 B7 A4
Pivot table: Pivot表:
tmp = pd.pivot_table(data=so, index='UserID', aggfunc=list)
AccID
UserID
A1 [B1, B2, B3]
A2 [B4, B5]
A3 [B6]
A4 [B7]
New columns from list:列表中的新列:
ans = pd.DataFrame(tmp['AccID'].to_list(), index=tmp.index)
0 1 2
UserID
A1 B1 B2 B3
A2 B4 B5 None
A3 B6 None None
A4 B7 None None
Changing the column names:更改列名:
ans.columns = [f"AccID{i + 1}" for i in ans.columns]
AccID1 AccID2 AccID3
UserID
A1 B1 B2 B3
A2 B4 B5 None
A3 B6 None None
A4 B7 None None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.