Python Pandas: Check If Items From List Is In Df Index
I have a dataframe: data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012], 'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions', 'Lions', 'Lions'],
Solution 1:
The reason you saw ['b','c']
in your first attempt is that what is returned from the inner isin
is a boolean index of [False, True, True]
which you're applying to the df from the beginning, you need to reapply it again to the last 3 rows:
In [21]:
fixed_cats = ['d','g','ssa']
football[-len(fixed_cats):][football.index[-len(fixed_cats):].isin(fixed_cats)]
Out[21]:
year team wins losses
g 2011 Lions 20%6%
ssa 2012 Lions 4%12%
In [22]:
football.index[-len(fixed_cats):].isin(fixed_cats)
Out[22]:
array([False, True, True], dtype=bool)
So the above boolean index needs to be applied to the last 3 rows rather to the entire df again which is what you are doing
Post a Comment for "Python Pandas: Check If Items From List Is In Df Index"