How can I slice a MultiIndex by its string content? I.e. whether that particular index contains a certain string?
In [12]: df = pd.DataFrame({'a': ['a', 'ab', 'b'],
'c': ['d', 'd', 'd'],
'val': [1, 2 , 3]}).set_index(['a', 'c'])
In [13]: df
Out[13]:
val
a c
a d 1
ab d 2
b d 3
In [15]: df.xs('a', level='a', drop_level=False)
Out[15]:
val
a c
a d 1
In[16]: df.xs(contains('a'), level='a', drop_level=False)
Expected output:
Out[16]:
a c
a d 1
ab d 2
Obviously that last bit is not possible.
Use boolean indexing
with get_level_values
and str.contains
:
print (df.index.get_level_values('a'))
Index(['a', 'ab', 'b'], dtype='object', name='a'
print (df.index.get_level_values('a').str.contains('a'))
[ True True False]
df1 = df[df.index.get_level_values('a').str.contains('a', case=False)]
print (df1)
val
a c
a d 1
ab d 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With