Reputation: 57
I have a dataframe
whose columns I want to drop can be listed by using df._get_numeric_data()
.
If I write
for df.column in df._get_numeric_data():
print(df.column)
I will get a list of all the columns in type string I want to drop.
Unnamed: 2
Unnamed: 4
Unnamed: 6
Unnamed: 8
Unnamed: 10
Unnamed: 12
Unnamed: 13
Unnamed: 15
Unnamed: 19
Unnamed: 21
Unnamed: 22
Unnamed: 24
Unnamed: 26
Unnamed: 28
Week Ending
Unnamed: 31
Unnamed: 34
Unnamed: 35
Unnamed: 37
Unnamed: 42
Unnamed: 47
I'm trying to figure out how to drop all those columns in one go. I tried
for df.column in df._get_numeric_data():
df.drop(df.column)
But get an error:
KeyError: "['Unnamed: 2'] not found in axis"
I tried a few other things, and think I'm not far off, but something about the df.drop(df.column)
isn't correct. Any suggestions?
Upvotes: 1
Views: 405
Reputation: 323386
You should add axis=1
, drop
default is to drop the index
for df.column in df._get_numeric_data():
df.drop(df.column,axis=1)
Also no need for loop
df=df.drop(df._get_numeric_data().column,axis=1)
Upvotes: 4