删除NaN行在 pandas 中不起作用 [英] Dropping NaN rows doesn't work in pandas

查看:55
本文介绍了删除NaN行在 pandas 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个约有7k行和4列的文件.许多单元格是空的,我尝试使用许多pandas函数将其删除,但似乎无济于事.我尝试过的函数和代码如下:

I have a file with about 7k rows and 4 columns. A lot of the cells are empty and I have tried to drop them using a number of pandas functions but nothing seems to work. Functions I have tried and the code are below:

我尝试过的事情:

df = df.dropna(thresh=2) 

df.dropna(axis=0, how='all')

我的代码:

file = "pc-dirty-data.csv"
path = root + file
name_cols = ['GUID1', 'GUID2', 'Record ID', 'Name', 'Org Name', 'Title']
pull_cols = ['Record ID', 'Name', 'Org Name', 'Title']
df = df.dropna(thresh=2) 
df.dropna(axis=0, how='all')
df = pd.read_csv(path, header=None, encoding="ISO-8859-1", names=name_cols, usecols=pull_cols, index_col=False)
df.info()

数据框:

RangeIndex: 6599 entries, 0 to 6598
Data columns (total 4 columns):
Record ID    5874 non-null float64
Name         5874 non-null object
Org Name     5852 non-null object
Title        5615 non-null object
dtypes: float64(1), object(3)

推荐答案

dropna不是就地操作,您需要将其重新分配给变量或使用设置为True的inplace参数.

dropna is not an inplace operation, you need to reassign it back to the variable or use the inplace parameter set to True.

df = df.dropna(axis=0, how='all')

df.dropna(axis=0, how='all', inplace=True)

编辑

Jay在评论中指出,您需要对代码逻辑进行重新排序,以使您可以在read_csv之后停顿.

Edit

Jay points out in the comments that, you need to reorder you code logic such that you dropna after the read_csv.

这篇关于删除NaN行在 pandas 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆