Pandas 熊猫:将列添加到多索引列数据帧

Pandas 熊猫:将列添加到多索引列数据帧,pandas,multi-index,Pandas,Multi Index,我想向多索引列数据帧的第二级添加一列 In [151]: df Out[151]: first bar baz second one two one two A 0.487880 -0.487661 -1.030176 0.100813 B 0.267913 1.918923 0.132791 0.178503 C 1.550526

我想向多索引列数据帧的第二级添加一列

In [151]: df
Out[151]: 
first        bar                 baz           
second       one       two       one       two 
A       0.487880 -0.487661 -1.030176  0.100813 
B       0.267913  1.918923  0.132791  0.178503
C       1.550526 -0.312235 -1.177689 -0.081596 
通常的直接分配技巧不起作用:

In [152]: df['bar']['three'] = [0, 1, 2]

In [153]: df
Out[153]: 
first        bar                 baz           
second       one       two       one       two 
A       0.487880 -0.487661 -1.030176  0.100813
B       0.267913  1.918923  0.132791  0.178503
C       1.550526 -0.312235 -1.177689 -0.081596

如何将第三行添加到“bar”下?

它实际上非常简单(FWIW,我原本以为是按您的方式进行的):


如果要添加多级列,请执行以下操作:

资料来源:

In [221]: df
Out[221]:
first        bar                 baz
second       one       two       one       two
A      -1.089798  2.053026  0.470218  1.440740
B       0.488875  0.428836  1.413451 -0.683677
C      -0.243064 -0.069446 -0.911166  0.478370
选项1:添加分割结果:
bar/baz
作为新的
foo

In [222]: df = df.join(df[['bar']].div(df['baz']).rename(columns={'bar':'foo'}))

In [223]: df
Out[223]:
first        bar                 baz                 foo
second       one       two       one       two       one       two
A      -1.089798  2.053026  0.470218  1.440740 -2.317647  1.424980
B       0.488875  0.428836  1.413451 -0.683677  0.345873 -0.627250
C      -0.243064 -0.069446 -0.911166  0.478370  0.266761 -0.145172
选项2:添加带有三个“子列”的多级列:


谢谢我必须说,(对我来说)为什么新专栏只在使用sort_索引后才出现,这完全不明显。哦,对不起,这不是答案的一部分,只是我太挑剔了。实际上,只要您调用
df['bar','three']=[0,1,2]
,它就会显示出来。默认情况下,pandas将把它放在数据帧的末尾(在[baz,two]之后)。我只是想用另一个
bar
s看看。感谢您的解释。这将在子表“bar”后面添加新的列“three”。但是如果您想在子表“bar”中插入(而不是追加)这个新列,例如在“one”和“two”之间插入“three”,该怎么办?这里列的顺序并不重要。如果你想对它们进行重新排序,使它们显示“一、三、二”,你可以使用
df.loc[:,XX]
XX其中
XX有元组(“bar”、“one”)、(“bar”、“three”)等。我猜OP的意思是添加第三列。如何显示和独立列?我试过:
df=df.join(pd.DataFrame(np.random.rand(3,1),columns=pd.MultiIndex.from_product([['new']]),index=df.index))
是正确的方法吗?
In [222]: df = df.join(df[['bar']].div(df['baz']).rename(columns={'bar':'foo'}))

In [223]: df
Out[223]:
first        bar                 baz                 foo
second       one       two       one       two       one       two
A      -1.089798  2.053026  0.470218  1.440740 -2.317647  1.424980
B       0.488875  0.428836  1.413451 -0.683677  0.345873 -0.627250
C      -0.243064 -0.069446 -0.911166  0.478370  0.266761 -0.145172
In [235]: df = df.join(pd.DataFrame(np.random.rand(3,3),
     ...:                           columns=pd.MultiIndex.from_product([['new'], ['one','two','three']]),
     ...:                             index=df.index))

In [236]: df
Out[236]:
first        bar                 baz                 new
second       one       two       one       two       one       two     three
A      -1.089798  2.053026  0.470218  1.440740  0.274291  0.636257  0.091048
B       0.488875  0.428836  1.413451 -0.683677  0.668157  0.456931  0.227568
C      -0.243064 -0.069446 -0.911166  0.478370  0.333824  0.363060  0.949672