python 2.7.11
pandas 0.18.1
当我尝试这样做时:
idx = pd.MultiIndex.from_product([['Ia','Ib'],['i1','i2','i3']])
df = pd.DataFrame({'A':['c','b','b','a','b','a'],'B':[10,-20,50,40,None,50],'C':[100,50,-30,-50,70,40]},index=idx)
print df.reindex(index=['Ib','Ia'],columns=['B','A','D'],level=0)
然后:
Traceback (most recent call last):
File "test.py", line 8, in <module>
print df.reindex(index=['Ib','Ia'],columns=['B','A','D'],level=0)
File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 2741, in reindex
**kwargs)
File "/usr/local/lib/python2.7/site-packages/pandas/core/generic.py", line 2229, in reindex
fill_value, copy).__finalize__(self)
File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 2682, in _reindex_axes
limit, tolerance)
File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 2704, in _reindex_columns
tolerance=tolerance)
File "/usr/local/lib/python2.7/site-packages/pandas/indexes/base.py", line 2330, in reindex
return_indexers=True)
File "/usr/local/lib/python2.7/site-packages/pandas/indexes/base.py", line 2636, in _join_level
old_level = left.levels[level]
AttributeError: 'Index' object has no attribute 'levels'
是OK ???
答案 0 :(得分:0)
回答这个问题:不,那不行!它不是一个错误......真的。
考虑数据框df
:
df = pd.DataFrame(np.arange(8).reshape(4, 2),
pd.MultiIndex.from_product([['a', 'b'], ['C', 'D']]),
['One', 'Two'])
df
我可以像这样重新索引:
df.reindex(index=['a'], level=0)
# ^
# |
# Note the use of level here
当我这样做时:
df.reindex(columns=['Two'], level=0)
AttributeError: 'Index' object has no attribute 'levels'
很简单地告诉我,我不能在没有级别的索引上使用level
。
好的,好的。但是,如果我这样做:
df.reindex(index=['a'], columns=['Two'], level=0)
同样的错误。显然,reindex
假设我的level
参数转到列而不是索引,当我通过它们时。
让我们测试一下这个想法:
df.T.reindex(columns=['a'], index=['Two'], level=0)
噢,哦!同样的错误。那个理论已经出来了。也许,我必须在两个轴上都有级别才能在传递索引和列时使用level
参数。
df.columns = pd.MultiIndex.from_product([['Three'], ['One', 'Two']])
df
df.reindex(index=['a'], columns=['Three'], level=0)
成功!
似乎如果你传递level
,你传递的所有索引都必须有级别。并且,level
参数将应用于所有传递的索引。
这对你意味着什么?
这样做:
df.reindex(index=['Ib','Ia'], level=0).reindex(columns=['B','A','D'])