Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在创建合并多个列的新列时,如何在pandas中多次旋转数据帧?_Python_Pandas_Numpy_Data Analysis - Fatal编程技术网

Python 在创建合并多个列的新列时,如何在pandas中多次旋转数据帧?

Python 在创建合并多个列的新列时,如何在pandas中多次旋转数据帧?,python,pandas,numpy,data-analysis,Python,Pandas,Numpy,Data Analysis,我发现这是一个相当复杂的挑战,因为我需要将数据帧中的多个列合并在一起,然后多次透视表(我认为) 因此,提供的输入是我过滤掉的数据帧: name X Y 0 Mathematics House AB 0.123 0.111 2111 Physical Science Hut 0.124 0.112 9412 Primary Education LO 0.125 0.113 1234 Tertiary

我发现这是一个相当复杂的挑战,因为我需要将数据帧中的多个列合并在一起,然后多次透视表(我认为)

因此,提供的输入是我过滤掉的数据帧:

                       name      X      Y
0      Mathematics House AB  0.123  0.111
2111   Physical Science Hut  0.124  0.112
9412   Primary Education LO  0.125  0.113
1234   Tertiary Universitas  0.126  0.114
12411  Principle of Physics  0.127  0.115
12373  Chemical Industry A1  0.128  0.116
输出应该是这样的:

                  label  Z      
   Mathematics House AB  X  0.123
                         Y  0.111
   Physical Science Hut  X  0.124
                         Y  0.112
   Primary Education LO  X  0.125
                         Y  0.113
   Tertiary Universitas  X  0.126
                         Y  0.114
   Principle of Physics  X  0.127
                         Y  0.115
   Chemical Industry A1  X  0.128
                         Y  0.116

其中Z表示尚未创建的新列。我目前正在使用一种非常粗糙的技术,将一些列作为numpy数组,并尝试重建它。结果也不尽如人意。有没有一种不使用numpy直接操作数据帧的方法?它似乎是一个可以旋转多次的工具。目前我使用的方法是
df.pivot(index='name',columns='Z').T.unstack().T
,我以前让
df['Z']=''
——确实非常难看和粗糙,它没有得到我想要的内容。

这是
堆栈
而不是
pivot

df.set_index('name').stack()
Out[186]: 
name                  
MathematicsHouseAB   X    0.123
                     Y    0.111
PhysicalScienceHut   X    0.124
                     Y    0.112
PrimaryEducationLO   X    0.125
                     Y    0.113
TertiaryUniversitas  X    0.126
                     Y    0.114
PrincipleofPhysics   X    0.127
                     Y    0.115
ChemicalIndustryA1   X    0.128
                     Y    0.116
dtype: float64
编辑:


谢谢你的回复。有没有办法通过用标签Z重新标记X、Y列和浮点列(总共第二列和第三列)来执行任务的第二部分?我需要一个数据帧。我曾尝试使用
to_frame()
将其转换为数据帧,然后使用
df.columns=['Z']
将其转换为数据帧,但似乎Z位于浮点的顶部,而不是X和Ys。然而,我想知道它是如何在系列也只是为了学习。最好在两种方法中灵活使用。对于第三列,是否有特定的列名?有没有办法将这两列合并到一列Z下?在系列中这样做会更容易吗(如果是的话,怎么做?@OldselLearner1959是时候澄清了,系列只有一列……其他的是索引,而不是列。我设法发现rename_axis=['name','Z']是有效的。非常感谢。
df=df.set_index('name').stack()
df.index.names=['name', 'Z']
df
Out[263]: 
                           0
name                Z       
MathematicsHouseAB  X  0.123
                    Y  0.111
PhysicalScienceHut  X  0.124
                    Y  0.112
PrimaryEducationLO  X  0.125
                    Y  0.113
TertiaryUniversitas X  0.126
                    Y  0.114
PrincipleofPhysics  X  0.127
                    Y  0.115
ChemicalIndustryA1  X  0.128
                    Y  0.116