I've got a Pandas DF using Date as an index, it has data from 2017-01-01 to 2019-31-12. How can I drop all rows that have the year 2019 in them?

huangapple go评论54阅读模式
英文:

I've got a Pandas DF using Date as an index, it has data from 2017-01-01 to 2019-31-12. How can I drop all rows that have the year 2019 in them?

问题

删除所有包含2019的日期列,从2017-2019的索引数据中。

df_raw1 = df_raw1.drop(index=['2019'])

错误信息

KeyError                                  Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_152442933074.py in <module>
----> 1 df_raw1 = df_raw1.drop(index=['2019'])

KeyError: "['2019'] not found in axis"
英文:

Deleting all the columns of date which contain 2019 in the index data from 2017-2019

df_raw1 = df_raw1.drop(index=['2019'])

Error

KeyError                                  Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_152442933074.py in <module>
----> 1 df_raw1 = df_raw1.drop(index=['2019'])


KeyError: "['2019'] not found in axis"

答案1

得分: 1

很抱歉,没有示例数据集,所以我猜日期格式可能是yyyy-mm-dd2017-01-01。而且不太清楚日期是列名还是行值

我会使用以下代码之一:

df.loc[:, ~df.columns.str.contains('2019')]

df = df.drop(df.filter(regex='2019').columns, axis=1)
英文:

Unfortunately, there is no example dataset, so I am gonna guess the date format is yyyy-mm-dd or 2017-01-01. Also it's not clear, whether the date is column name or rather row value.

I would use

df.loc[:,~df.columns.str.contains('2019')] 

or

df = df.drop(df.filter(regex='2019').columns, axis=1)

huangapple
  • 本文由 发表于 2023年3月4日 04:48:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定