问题
I have searched several questions around this topic but have not found and answer that made my code work. I'm a beginner so any help is much appreciated.
I'm using the jupyter notebook and have the following code:
import pandas
a = pandas.read_csv("internal_html.csv")
a.dropna(axis="columns", how="any")
a.head(10)
I get no error when running the code, but the columns with NaN values still show up.
Thanks!
回答1:
You need to reassign the dropna statement back to a.
a = a.dropna(axis="columns", how="any")
dropna is not an inplace operation by default.
Or you could:
a.dropna(axis="columns", how="any", inplace=True)
Edit to handle empty values in source as mentioned in the comments below.
import pandas as pd
import numpy as np
a = pd.read_csv("internal_html.csv")
a = a.replace('',np.nan)
a = a.dropna(axis="columns", how="any")
a.head(10)
来源:https://stackoverflow.com/questions/44862408/removing-nan-values-from-csv