python - Pandas 删除数据框中不同行的列表

标签 python pandas

我有日期列表和数据框。我想删除列表中数据框的那些日期。

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/

相关文章:

python - 在pytest中模拟模块导入

python - 如何通过Selenium-Python访问 'rect'类型元素

python - 将重复项替换为数据框中的第一个值

python - 如何优化这个 numpy 代码以使其更快?

python - Pandas :Groupby 并在组内使用条件进行迭代?

Python Pandas - 具有不同列的 Concat 数据框忽略列名

python - shlex.split() 和 re.split() 有什么区别?

python - 如何获取每个字符串变体以及每个位置的可能性列表

python - Pandas Left Merge/Join not 导致左连接的预期结果

python - 带有列表和元组参数的 np.c_ 的行为