0

How do i remove nan values from dataframe in Python? I already tried with dropna(), but that did not work for me. Also is NaN diffferent from nan. I am using Pandas.

While printing the data frame it does not print as NaN but instead as nan.

1                  2.11358      0.649067060588935
2                  nan          0.6094130485307419
3                  2.10066      0.3653980276694516
4                  2.10545                     nan
talatccan
  • 743
  • 5
  • 19
  • Does this answer your question? [How to drop rows of Pandas DataFrame whose value in a certain column is NaN](https://stackoverflow.com/questions/13413590/how-to-drop-rows-of-pandas-dataframe-whose-value-in-a-certain-column-is-nan) – ishan Mar 05 '20 at 02:03

1 Answers1

1

You can change nan values with NaN using replace() and then use dropna().

import numpy as np

df = df.replace('nan', np.nan)
df = df.dropna()

Update:

Original dataframe:

1  2.11358   0.649067060588935
2      nan  0.6094130485307419
3  2.10066  0.3653980276694516
4  2.10545                 nan

Applied df.replace('nan', np.nan):

1  2.11358   0.649067060588935
2      NaN  0.6094130485307419
3  2.10066  0.3653980276694516
4  2.10545                 NaN

Applied df.dropna():

1  2.11358   0.649067060588935
3  2.10066  0.3653980276694516
talatccan
  • 743
  • 5
  • 19