2

A super simple question, for which I cannot find an answer.

I have a dataframe with 1000+ columns and cannot drop by column number, I do not know them. I want to drop all columns between two columns, based on their names.

foo = foo.drop(columns = ['columnWhatever233':'columnWhatever826']) 

does not work. I tried several other options, but do not see a simple solution. Thanks!

Anakin Skywalker
  • 2,400
  • 5
  • 35
  • 63
  • 2
    Did you mean to use a comma in your list... `foo = foo.drop(columns = ['columnWhatever233', 'columnWhatever826']) `? Other than the typo the syntax looks correct, can you demonstrate what you mean by "does not work"? – THK Jul 29 '21 at 18:48
  • No, I put `:` intentionally. If I understand, putting a comma would drop only two columns. – Anakin Skywalker Jul 29 '21 at 18:49
  • 1
    Looks like a duplicate of https://stackoverflow.com/questions/28538536/deleting-multiple-columns-based-on-column-names-in-pandas – Abinash Bishoyi Jul 29 '21 at 18:57
  • @AbinashBishoyi, of course I tried several options from that answer, but for some reason it did not work for me. – Anakin Skywalker Jul 29 '21 at 18:58

1 Answers1

2

You can use .loc with column range. For example if you have this dataframe:

   A  B  C  D  E
0  1  3  3  6  0
1  2  2  4  9  1
2  3  1  5  8  4

Then to delete columns B to D:

df = df.drop(columns=df.loc[:, "B":"D"].columns)
print(df)

Prints:

   A  E
0  1  0
1  2  1
2  3  4
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91