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

孤街浪徒 提交于 2020-02-17 12:39:42

问题


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


回答1:


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'])

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'])



回答2:


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()
    


来源:https://stackoverflow.com/questions/49291740/delete-rows-if-there-are-null-values-in-a-specific-column-in-pandas-dataframe

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