Trouble With Using Iloc In Pandas Dataframe With Hierarchical Index
I'm getting this ValueError whenever I try to give a list to iloc on a dataframe with a hierarchical index. I'm not sure if I'm doing something wrong or if this is a bug. I haven
Solution 1:
This was a bug and has been fixed in master (0.13), a temporary workaround is to use ix (!):
In[11]: df1.ix[[1, 3]]
Out[11]:
DEFay1.5445771.594976-0.084866by-0.4680740.673112-0.900547
In master, 0.13:
In[12]: df1.iloc[[1, 3]]
Out[12]:
DEFay1.5445771.594976-0.084866by-0.4680740.673112-0.900547
Solution 2:
It seems that pandas can't convert [[1,3]]
to a proper MultiIndex. You might want to fill a bug in pandas issues tracker. The only workaround I found is to construct it manually, this way it is passed as is.
>>>tup = zip(*[['a','a','b','b'],['x','y','x','y']])>>>index = pd.MultiIndex.from_tuples(tup, names=['f','s'])>>>df = pd.DataFrame(np.random.randn(4, 4))>>>df
0 1 2 3
f s
a x -0.334280 0.479317 -0.358416 -0.245807
y 1.279348 -0.096336 0.100285 0.037231
b x -0.368452 0.219868 -0.103722 -0.575399
y -0.813583 -0.042694 0.897361 1.636304
>>>idx = [i in [1,3] for i inrange(len(df.index))]>>>idx
[False, True, False, True]
>>>df.iloc[idx]
0 1 2 3
f s
a y 1.279348 -0.096336 0.100285 0.037231
b y -0.813583 -0.042694 0.897361 1.636304
Other ways is to use get_level_values
to access MultiIndex
by level
>>> df.iloc[df.index.get_level_values('f') == 'a']0123
f s
a x -0.3342800.479317 -0.358416 -0.245807
y 1.279348 -0.0963360.1002850.037231
On contrast, slice is correctly converted to MultiIndex:
>>> df.iloc[0:2,:]0123
f s
a x -0.334280.479317 -0.358416 -0.245807a y 1.279348 -0.0963360.1002850.037231
Post a Comment for "Trouble With Using Iloc In Pandas Dataframe With Hierarchical Index"