Home > Mobile >  Pandas - how to remove rows with nulls based on one column
Pandas - how to remove rows with nulls based on one column

Time:12-11

I have a table:

|    A     |       B        |
| -------- | -------------- |
| Cell 1   | np.nan         |
| Cell 2   | np.nan         |
| np.nan   | Cell 3         |
| Cell 4   | Cell 4         |

For example, I need to delete those rows where np.nan is present in table B

My code #1:

df = df[df['B'] != np.nan]

My code #2:

df = df.drop(df[
        df['B']
     ].index)

CodePudding user response:

This function will drop your nulls:

df = df.dropna(subset=‘B’)
  • Related