[英]convert column into headings for dataframe
I have this df (exceprt shown below) which has a column called GRP with data in other columns. 我有这个df(如下所示的exceprt),其中有一个名为GRP的列,其他列中有数据。 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. 我想做的是将其他列按Grp值除以Grp值作为列标题。
Original DF 原始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: 如果按Grp
,则可以添加“索引”,它是每个组中项目数的简单累积计数:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.