I have a data frame with 69 columns but I only want to keep 20 of those columns. I want to drop the first column, then the next 20 columns are the ones I want to keep, and then all the columns after that should be dropped. The data frame is called df. I tried using .drop() but when I do df.drop(df.columns[0], inplace=True)
it gives this error KeyError: "['World Development Indicators'] not found in axis"
. ['World Development Indicators'] is the name of the first column. When I try to do the same thing to delete multiple rows it gives the same error but lists all the other columns I am trying to delete.
CodePudding user response:
You can keep/drop the columns on index position using iloc
:
df.iloc[:, 1:21]
This drops the first column (index 0) and keeps the next 20.
CodePudding user response:
pd.DataFrame({"column1": [1,2,3], "column2": [3,4,5]}).drop("column1")
# output
KeyError: "['column1'] not found in axis"
But when you add axis=1:
pd.DataFrame({"column1": [1,2,3], "column2": [3,4,5]}).drop("column1", axis=1)
Column will be removed successfully.
CodePudding user response:
You have to specify the columns parameter:
df.drop(columns=df.columns[0], inplace=True)
should work
CodePudding user response:
To drop, for example, columns 0,20-29,35,40-49, you can use:
todrop = [0,35] list(range(20, 30)) list(range(40, 50))
df.drop(columns=df.iloc[:, todrop], inplace=True)