Delete rows pandas Dataframe based on index (multiple criteria) (Python 3.5.1)
Call drop
and pass a list on level='county
to drop row labels with those values on that index level:
In [284]:
df.drop(['D','G',np.NaN], level='county')
Out[284]:
population
state county
NJ A 100
B 200
CA E 500
F 600
You could try using the inverse operator (~) on boolean indexing. For example,
import numpy as np
df[~(df.index.get_level_values('county').isin(['A', 'B', np.nan]))]
this line of code says "select from df where county is NOT in some list"
网友评论