英文:
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-dd
或2017-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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论