0

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_15244\372933074.py in <module>
----> 1 df_raw1 = df_raw1.drop(index=['2019'])


KeyError: "['2019'] not found in axis"
FN_
  • 715
  • 9
  • 27
  • Hi, welcome. It will help if you can provide dataframe example. Without this it is impossible to send an accurate answer. – FN_ Mar 03 '23 at 20:51
  • I have meteorological data from 2017-01-01 to 2019-12-31, but i only need data from 2017-2018, I need to drop all the values for the year 2019. – Venkatesh Munukutla Mar 03 '23 at 20:54
  • 1
    Please [edit] your post and actually include a sample of your data so we can see the names of your index/columns and format of values. Just run `df_raw1.head(10).to_dict()`, for example. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391). – AlexK Mar 03 '23 at 23:56

1 Answers1

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)
FN_
  • 715
  • 9
  • 27