Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

python - pandas: unable to remove the second column

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


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

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

@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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...