如果Pandas数据框中的特定列中有空值,请删除行 [英] Delete rows if there are null values in a specific column in Pandas dataframe

查看:697
本文介绍了如果Pandas数据框中的特定列中有空值,请删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是python熊猫的新手.在删除具有空值的几行时需要一些帮助.在屏幕截图中,我需要使用python pandas删除charge_per_line =-的行.谢谢!!

Am new to python pandas. Need some help with deleting few rows where there are null values. In the screenshot, I need to delete rows where charge_per_line = - using python pandas. Thanks !!

推荐答案

如果在阅读熊猫时Charge_Per_Line中的相关条目为空(NaN),则可以使用df.dropna:

If the relevant entries in Charge_Per_Line are empty (NaN) when you read into pandas, you can use df.dropna:

df = df.dropna(axis=0, subset=['Charge_Per_Line'])

如果值是真正的-,则可以将其替换为np.nan,然后使用df.dropna:

If the values are genuinely -, then you can replace them with np.nan and then use df.dropna:

import numpy as np

df['Charge_Per_Line'] = df['Charge_Per_Line'].replace('-', np.nan)
df = df.dropna(axis=0, subset=['Charge_Per_Line'])

这篇关于如果Pandas数据框中的特定列中有空值,请删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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