美文网首页
Delete rows pandas Dataframe bas

Delete rows pandas Dataframe bas

作者: 榴莲气象 | 来源:发表于2019-01-12 12:04 被阅读2次

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"

相关文章

网友评论

      本文标题:Delete rows pandas Dataframe bas

      本文链接:https://www.haomeiwen.com/subject/iqoqdqtx.html