how to drop multiple (~5000) columns in the pandas dataframe?
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.
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?