Setting DataFrame column headers to a MultiIndex

允我心安 提交于 2019-11-30 17:37:44

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!