I want to convert from a long to a wide table with dummy column names created based on the number of accid
sample excel input vs output attached
Please help
Advertisement
Answer
I was able to get down to 2 steps, pivot_table
using aggfunc=list
, and then creating new columns from that 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:
JavaScript
x
13
13
1
so = pd.DataFrame({'AccID': 'B1 B2 B3 B4 B5 B6 B7'.split(),
2
'UserID': 'A1 A1 A1 A2 A2 A3 A4'.split()}
3
)
4
5
AccID UserID
6
0 B1 A1
7
1 B2 A1
8
2 B3 A1
9
3 B4 A2
10
4 B5 A2
11
5 B6 A3
12
6 B7 A4
13
Pivot table:
JavaScript
1
9
1
tmp = pd.pivot_table(data=so, index='UserID', aggfunc=list)
2
3
AccID
4
UserID
5
A1 [B1, B2, B3]
6
A2 [B4, B5]
7
A3 [B6]
8
A4 [B7]
9
New columns from list:
JavaScript
1
9
1
ans = pd.DataFrame(tmp['AccID'].to_list(), index=tmp.index)
2
3
0 1 2
4
UserID
5
A1 B1 B2 B3
6
A2 B4 B5 None
7
A3 B6 None None
8
A4 B7 None None
9
Changing the column names:
JavaScript
1
9
1
ans.columns = [f"AccID{i + 1}" for i in ans.columns]
2
3
AccID1 AccID2 AccID3
4
UserID
5
A1 B1 B2 B3
6
A2 B4 B5 None
7
A3 B6 None None
8
A4 B7 None None
9