如何将嵌套列添加到3D Pandas DataFrame?

问题描述 投票:1回答:1

Pandas的新手,不太确定3D DataFrame的工作原理。我的数据框称为'new',如下所示:

     unique    cat    numerical   
          a  b   c  d         e  f
   0      0  1   2  3         4  5
   1      0  1   2  3         4  5

我想插入列'z',以便它最终像这样:

     unique       cat    numerical   
          a  b  z   c  d         e  f
   0      0  1  9   2  3         4  5
   1      0  1  9   2  3         4  5

从我的数据框中切出'unique'后,我成功地创建了一个新专栏:

这样做:

new_column = new.loc[:,'unique'].assign(z=pd.Series([9,9]).values)

得到我这个:

   a  b  z
0  0  1  9
1  0  1  9

但是我不知道如何将它放回数据帧。我试过了:

new['unique'] = new_column

但我发现它只是试图替换'unique'下找到的所有行和列中的所有值,如下所示:

new['unique'] = 'a'

获取:

  unique    cat    numerical   
       a  b   c  d         e  f
0      a  a   2  3         4  5
1      a  a   2  3         4  5

而使用.loc取而代之的是:

  unique     cat    numerical   
       a   b   c  d         e  f
0    NaN NaN   2  3         4  5
1    NaN NaN   2  3         4  5

这是我的完整代码:

import pandas as pd
import numpy as np

data=[[0,1,2,3,4,5],[0,1,2,3,4,5]]
datatypes=np.array(['unique','unique','cat','cat','numerical','numerical'])
columnnames=np.array(['a','b','c','d','e','f'])
new = pd.DataFrame(data=data, columns=pd.MultiIndex.from_tuples(zip(datatypes,columnnames)))
print('new: ')
print(new)

new_column = new.loc[:,'unique'].assign(z=pd.Series([9,9]).values)
print('\nnew column:')
print(new_column)

new.loc[:,'unique'] = new_column
print('\nattempt 1:')
print(new)

new['unique'] = new_column
print('\nattempt 2:')
print(new)
python python-3.x pandas numpy dataframe
1个回答
1
投票

一种方法:

# Create your new multiindexed column:
new['unique','z'] = 9
# Re-order your columns in your desired order:
new = new[['unique', 'cat', 'numerical']]

>>> new
  unique       cat    numerical   
       a  b  z   c  d         e  f
0      0  1  9   2  3         4  5
1      0  1  9   2  3         4  5
© www.soinside.com 2019 - 2024. All rights reserved.