0

My DataFrame has two columns, both of which have NaN values. I need to delete the rows with NaN just on the column user_email.

However, I used df['user_email'] = df['user_email'].dropna() but it returned the exact same DataFrame, with all the NaN values on the second column intact.

How can I delete the rows with NaN on the second column? Here's my DF:

Adamastor
  • 11
  • 3

3 Answers3

0

You could use boolean indexing. This allows you to select rows based on a conditional statement (e.g., df.user_email.notna())

df = df[df.user_email.notna()]
kait
  • 1,327
  • 9
  • 13
  • 1
    Code only answers can generally be improved by adding some explanation of how and why they work. Without that explanation it looks like a low quality answer when it hits the review queues. – Jason Aller Jul 04 '20 at 05:14
  • Thanks for the feedback. I've updated my answer to include an explanation. – kait Jul 04 '20 at 14:28
0

You can use the subset keyword argument.

df = df.dropna(subset=['user_email'])
Joooeey
  • 3,394
  • 1
  • 35
  • 49
pecey
  • 651
  • 5
  • 13
0

You may need inplace=True:

df.dropna(subset=['user_email'], inplace=True)
naccode
  • 510
  • 1
  • 8
  • 18