0

How do I remove columns starting from a certain index to another, I need to remove columns from 1950 to 2004?

I tried running this but it didn’t work

test = df.drop(df.iloc[:, 1:55],  axis = 1)
sushanth
  • 8,275
  • 3
  • 17
  • 28
  • `df.drop(list(range(1950,2005)), axis=1, errors='ignore')`? – mozway Nov 30 '21 at 05:31
  • https://stackoverflow.com/a/48671822/1560708 <-- this does not solve your problem? – Shahan M Nov 30 '21 at 05:34
  • Does this answer your question? [python dataframe pandas drop column using int](https://stackoverflow.com/questions/20297317/python-dataframe-pandas-drop-column-using-int) – Shahan M Nov 30 '21 at 05:37

1 Answers1

0

Assuming the column names are integers:

test = df.drop(list(range(1950,2005)), axis=1, errors='ignore')

For strings:

test = df.drop(list(map(str, range(1950,2005))), axis=1, errors='ignore')
mozway
  • 194,879
  • 13
  • 39
  • 75