Pandas 来自 MultiIndex 和 NumPy 结构化数组的 DataFrame (recarray)

Pandas DataFrame from MultiIndex and NumPy structured array (recarray)

首先我创建了一个两级MultiIndex:

import numpy as np
import pandas as pd

ind = pd.MultiIndex.from_product([('X','Y'), ('a','b')])

我可以这样使用:

pd.DataFrame(np.zeros((3,4)), columns=ind)

给出:

     X         Y     
     a    b    a    b
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0

但现在我正在尝试这样做:

dtype = [('Xa','f8'), ('Xb','i4'), ('Ya','f8'), ('Yb','i4')]
pd.DataFrame(np.zeros(3, dtype), columns=ind)

但这给出了:

Empty DataFrame
Columns: [(X, a), (X, b), (Y, a), (Y, b)]
Index: []

我期望的结果与之前的结果类似,有三行。

也许更一般地说,我想做的是生成一个 Pandas DataFrame,其中包含 MultiIndex 列,其中列具有不同的类型(如示例中,a 是浮点数,但 b 是整数)。

这看起来像是一个错误,值得报告 as an issue github

解决方法是在构造后手动设置列:

In [11]: df1 = pd.DataFrame(np.zeros(3, dtype))

In [12]: df1.columns = ind

In [13]: df1
Out[13]:
     X       Y
     a  b    a  b
0  0.0  0  0.0  0
1  0.0  0  0.0  0
2  0.0  0  0.0  0
pd.DataFrame(np.zeros(3, dtype), columns=ind)

Empty DataFrame
Columns: [(X, a), (X, b), (Y, a), (Y, b)]
Index: []

只是显示数据帧输出的文本表示。

Columns: [(X, a), (X, b), (Y, a), (Y, b)]

就是索引的文本表示。

如果你改为:

df = pd.DataFrame(np.zeros(3, dtype), columns=ind)

print type(df.columns)

<class 'pandas.indexes.multi.MultiIndex'>

你看确实是一个pd.MultiIndex

话虽如此,但请让开。我不明白的是为什么在数据框构造函数中指定索引会删除值。

解决方法是这样。

df = pd.DataFrame(np.zeros(3, dtype))

df.columns = ind

print df

     X       Y   
     a  b    a  b
0  0.0  0  0.0  0
1  0.0  0  0.0  0
2  0.0  0  0.0  0