Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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 将DataFrame列标题设置为多索引_Python_Pandas_Multi Index - Fatal编程技术网

Python 将DataFrame列标题设置为多索引

Python 将DataFrame列标题设置为多索引,python,pandas,multi-index,Python,Pandas,Multi Index,如何将具有单级列的现有数据帧转换为具有层次索引列(多索引) 数据帧示例: In [1]: import pandas as pd from pandas import Series, DataFrame df = DataFrame(np.arange(6).reshape((2,3)), index=['A','B'], columns=['one','two','three']) df Out [1]: one two

如何将具有单级列的现有数据帧转换为具有层次索引列(多索引)

数据帧示例:

In [1]:
import pandas as pd
from pandas import Series, DataFrame

df = DataFrame(np.arange(6).reshape((2,3)),
               index=['A','B'],
               columns=['one','two','three'])
df
Out [1]:
   one  two  three
A    0    1      2
B    3    4      5
我本以为reindex()会起作用,但我得到了NaN的:

In [2]:
df.reindex(columns=[['odd','even','odd'],df.columns])
Out [2]:
   odd  even    odd
   one   two  three
A  NaN   NaN    NaN
B  NaN   NaN    NaN
如果使用DataFrame(),则相同:

如果我指定df.values,最后一种方法实际上是有效的:

In [4]:
DataFrame(df.values,index=df.index,columns=[['odd','even','odd'],df.columns])
Out [4]:
   odd  even    odd
   one   two  three
A    0     1      2
B    3     4      5

正确的方法是什么?为什么reindex()会给出NaN的?

您已经接近了,只需将列直接设置为一个新的(大小相等的)索引,如(如果是列表,则会转换为多索引)


Reindex将对现有索引进行重新排序/筛选。得到所有NaN的原因是,查找与新索引匹配的现有列;无匹配项,因此如果要使用numpy数组,则首先将其转换为list:
df.columns=list(a)
In [4]:
DataFrame(df.values,index=df.index,columns=[['odd','even','odd'],df.columns])
Out [4]:
   odd  even    odd
   one   two  three
A    0     1      2
B    3     4      5
In [8]: df
Out[8]: 
   one  two  three
A    0    1      2
B    3    4      5

In [10]: df.columns = [['odd','even','odd'],df.columns]

In [11]: df
Out[11]: 
   odd  even    odd
   one   two  three
A    0     1      2
B    3     4      5