Python 如果 Pandas 数据框中的特定列中存在空值,则删除行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49291740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:02:29  来源:igfitidea点击:

Delete rows if there are null values in a specific column in Pandas dataframe

pythonpandasdataframefilterseries

提问by kumar

enter image description here

在此处输入图片说明

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 !!

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

回答by jpp

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

如果NaN读入pandas时Charge_Per_Line中的相关条目为空( ),则可以使用df.dropna

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

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

如果这些值是真的-,那么您可以将它们替换为np.nan,然后使用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'])

回答by Vaishali

Multiple ways

多种方式

  1. Use str.contains to find rows containing '-'

    df[~df['Charge_Per_Line'].str.contains('-')]
    
  2. Replace '-' by nan and use dropna()

    df.replace('-', np.nan, inplace = True)
    df = df.dropna()
    
  1. 使用 str.contains 查找包含“-”的行

    df[~df['Charge_Per_Line'].str.contains('-')]
    
  2. 用 nan 替换 '-' 并使用 dropna()

    df.replace('-', np.nan, inplace = True)
    df = df.dropna()