问题
How do I convert an existing dataframe with single-level columns to have hierarchical index columns (MultiIndex)?
Example dataframe:
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
I'd have thought that reindex() would work, but I get NaN's:
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
Same if I use DataFrame():
In [3]:
DataFrame(df,columns=[['odd','even','odd'],df.columns])
Out [3]:
odd even odd
one two three
A NaN NaN NaN
B NaN NaN NaN
This last approach actually does work if I specify 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
What's the proper way to do this? Why does reindex() give NaN's?
回答1:
You were close, just set the columns directly to a new (equal sized) index-like (which if its a list-of-list will convert to a multi-index)
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
Reindex will reorder / filter the existing index. The reason you get all nans is you are saying, hey find the existing columns that match this new index; none match, so that's what you get
来源:https://stackoverflow.com/questions/18262962/setting-dataframe-column-headers-to-a-multiindex