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
Delete rows if there are null values in a specific column in Pandas dataframe
提问by kumar
回答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.nan
and 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
多种方式
Use str.contains to find rows containing '-'
df[~df['Charge_Per_Line'].str.contains('-')]
Replace '-' by nan and use dropna()
df.replace('-', np.nan, inplace = True) df = df.dropna()
使用 str.contains 查找包含“-”的行
df[~df['Charge_Per_Line'].str.contains('-')]
用 nan 替换 '-' 并使用 dropna()
df.replace('-', np.nan, inplace = True) df = df.dropna()