我有日期列表和数据框。我想删除列表中数据框的那些日期。
bad_dates = ['2019-01-24', '2019-01-29']
df = r2POADC r2DCAC r2ACPOA ... good1 bad bad1
2019-01-24 0.931928 0.953512 0.952798 ... False False True
2019-01-25 -1.681725 -2.163356 0.961674 ... False False True
2019-01-26 -0.879915 -1.398238 0.911883 ... False False True
2019-01-28 -1.637134 -0.718831 0.783878 ... False False True
2019-01-29 -1.839277 -6.368115 0.951883 ... False False True
2019-01-30 -0.188997 -0.566269 0.919461 ... False False True
我的代码是:
df.drop([bad_dates],axis=0,inplace=True)
我当前的输出是:
KeyError: "[('2019-01-24', '2019-01-29')] not found in axis"
这很有趣,因为两个日期都出现在索引中,但它表示在轴(索引)中都找不到。
我想要以下输出:
df = r2POADC r2DCAC r2ACPOA ... good1 bad bad1
2019-01-25 -1.681725 -2.163356 0.961674 ... False False True
2019-01-26 -0.879915 -1.398238 0.911883 ... False False True
2019-01-28 -1.637134 -0.718831 0.783878 ... False False True
2019-01-30 -0.188997 -0.566269 0.919461 ... False False True
看起来像!我的代码中有一个小错误。
最佳答案
在处理日期时,pandas 大多数时候会隐式地将字符串转换为日期。但是,drop
显然是一个异常(exception),因此它需要显式转换:
df.drop(pd.to_datetime(bad_dates), axis=0, inplace=True)
关于python - Pandas 删除数据框中不同行的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55818051/