为什么示例 1 会返回 NaN,而示例 2 不会?
示例 1:
data=DataFrame(np.arange(0,16).reshape(4,4),
index=[list('abcd')],
columns=[list('retz')])
data[data['t'] > 5]
r e t z
a NaN NaN NaN NaN
b NaN NaN 6.0 NaN
c NaN NaN 10.0 NaN
d NaN NaN 14.0 NaN
示例 2:
data2 = DataFrame(np.arange(16).reshape((4, 4)),
index=['Ohio', 'Colorado', 'Utah', 'New York'],
columns=['one', 'two', 'three', 'four'])
data2[data2['three'] > 5]
one two three four
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
第一个数据帧具有多索引
data.axes
> [MultiIndex(levels=[['a', 'b', 'c', 'd']],
labels=[[0, 1, 2, 3]]), MultiIndex(levels=[['e', 'r', 't', 'z']],
labels=[[1, 0, 2, 3]])]
而你的第二个没有:
data2.axes
> [Index(['Ohio', 'Colorado', 'Utah', 'New York'], dtype='object'),
Index(['one', 'two', 'three', 'four'], dtype='object')]
这是因为您已将list('retz')
包装在另一个列表中,因此它被解释为 [['e', 'r', 't', 'z']]
。如果你只想有一个索引,你只需去掉括号。
data=DataFrame(np.arange(0,16).reshape(4,4),
index=list('abcd'),
columns=list('retz'))
data[data['t'] > 5]
> r e t z
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15