1

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?

0

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.