Removing NaN Values from csv

旧时模样 提交于 2021-02-20 04:54:46

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!