0

I have a dataframe with 5632 columns, and I only want to keep 500 of them. I have the columns names (that I wanna keep) in a dataframe as well, with the names as the row index. Is there any way to do this?

Rachel
  • 1
  • 1
  • 2
    Probably just use `.loc`: `df.loc[:, df1.index]`, but please provide a [mcve] so we can provide a suitable answer. It doesn't need to be all 5000+ columns, an example with say 10 will probably suffice. – ALollz Mar 25 '19 at 03:20
  • Possible duplicate of [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – jonboy Mar 25 '19 at 05:12

1 Answers1

0

Let us assume your DataFrame is named as df and you have a list cols of column indices you want to retain. Then you should use:

df1 = df.iloc[:, cols]

This statement will drop all the columns other than the ones whose indices have been specified in cols. Use df1 as your new DataFrame.

nikhilbalwani
  • 817
  • 7
  • 20