I have this df (exceprt shown below) which has a column called GRP with data in other columns. What I'd like to do is have the other columns split out by the value of Grp w9th the Grp value as a column heading.
Original DF
>>> xx
x1 Grp
2 3.670 8ZD (S00K49)
3 3.659 8ZD (S00K49)
4 3.576 8ZD (S00K49)
5 3.509 8ZD (S00K49)
10 3.879 MPN 603 (Pos 1)
11 3.816 MPN 603 (Pos 1)
12 3.881 MPN 603 (Pos 1)
17 3.813 MPN 604 (Pos 1)
20 3.670 8ZD (S00K49)
21 3.612 8ZD (S00K49)
36 3.774 MPN 603 (Pos 1)
37 3.752 MPN 603 (Pos 1)
38 3.667 MPN 603 (Pos 1)
39 3.717 MPN 603 (Pos 1)
40 3.730 MPN 604 (Pos 1)
41 3.771 MPN 604 (Pos 1)
42 3.621 MPN 604 (Pos 1)
Desired Result:
>>> yy
8ZD (S00K49) MPN 603 (Pos 1) MPN 604 (Pos1)
2 3.670 3.879 3.881
3 3.659 3.816 3.813
4 3.576 NaN NaN
5 3.509 NaN NaN
I've been unable to find how to do this either online or in the documentation. Experimented a bit with the stack and unstack methods but never got what I was looking for that way. ANy tips much appreaciated.
If you groupby by Grp
, you can add an "index" which is a simple cumulative count of the number of items in each group:
In [18]: df['index'] = df.groupby(['Grp']).cumcount()
In [19]: df
Out[19]:
x1 Grp index
2 3.670 8ZD (S00K49) 0
3 3.659 8ZD (S00K49) 1
4 3.576 8ZD (S00K49) 2
5 3.509 8ZD (S00K49) 3
10 3.879 MPN 603 (Pos 1) 0
11 3.816 MPN 603 (Pos 1) 1
12 3.881 MPN 603 (Pos 1) 2
17 3.813 MPN 604 (Pos 1) 0
20 3.670 8ZD (S00K49) 4
21 3.612 8ZD (S00K49) 5
36 3.774 MPN 603 (Pos 1) 3
37 3.752 MPN 603 (Pos 1) 4
38 3.667 MPN 603 (Pos 1) 5
39 3.717 MPN 603 (Pos 1) 6
40 3.730 MPN 604 (Pos 1) 1
41 3.771 MPN 604 (Pos 1) 2
42 3.621 MPN 604 (Pos 1) 3
In [23]: result = df.pivot(index='index',
columns='Grp', values='x1')
yields
In[24]: result
Out[24]:
Grp 8ZD (S00K49) MPN 603 (Pos 1) MPN 604 (Pos 1)
index
0 3.670 3.879 3.813
1 3.659 3.816 3.730
2 3.576 3.881 3.771
3 3.509 3.774 3.621
4 3.670 3.752 NaN
5 3.612 3.667 NaN
6 NaN 3.717 NaN
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.