将DataFrame列标题设置为MultiIndex [英] Setting DataFrame column headers to a MultiIndex

查看:84
本文介绍了将DataFrame列标题设置为MultiIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将具有单级列的现有数据框转换为具有层次结构的 index 列(MultiIndex)?

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

示例数据框:

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:

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

与使用DataFrame()相同:

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

如果我指定df.values,则最后一种方法确实有效:

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

执行此操作的正确方法是什么?为什么reindex()给出NaN?

What's the proper way to do this? Why does reindex() give NaN's?

推荐答案

您已经很接近了,只需将列直接设置为新的(等长)索引(例如,如果其列表将转换为多索引)

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将对现有索引重新排序/过滤.获得所有Nan的原因是您在说,嘿,找到与该新索引匹配的现有列.没有匹配项,那就是您得到的

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

这篇关于将DataFrame列标题设置为MultiIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆