Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am attempting to remove some columns from a DataFrame. I can successfully remove the first column but when I attempt to remove the second, it failed and I am not sure why.

Code:

df.drop(columns=["StockName", "DirectorName"])

I have also attempted with the same outcome:

df.drop(["StockName", "DirectorName"], 1)

Error Message: screenshot of the error message


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
263 views
Welcome To Ask or Share your Answers For Others

1 Answer

@jezrael was correct with the df.columns = df.columns.str.strip(). This worked successfully. I will check to see if there are any excess spaces in the orignal file the DataFrame was created from.

TY all


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...