36

enter image description here

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

maciejwww
  • 758
  • 10
  • 22
kumar
  • 529
  • 1
  • 5
  • 9

2 Answers2

60

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'])
jpp
  • 147,904
  • 31
  • 244
  • 302
6

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()
    
cs95
  • 330,695
  • 80
  • 606
  • 657
Vaishali
  • 35,413
  • 4
  • 48
  • 78