pandas 将 DataFrame 列标题设置为 MultiIndex

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18262962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:05:28  来源:igfitidea点击:

Setting DataFrame column headers to a MultiIndex

pythonpandasmulti-index

提问by mqk

How do I convert an existing dataframe with single-level columns to have hierarchical indexcolumns (MultiIndex)?

如何将具有单级列的现有数据框转换为具有分层索引列 (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:

我原以为 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

Same if I use DataFrame():

如果我使用 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:

如果我指定 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?

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

回答by Jeff

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

Reindex 将重新排序/过滤现有索引。你得到所有 nans 的原因是你说,嘿,找到与这个新索引匹配的现有列;没有匹配,所以这就是你得到的