I have an dataframe contains NaN values and some data. I want to remove NaN values from my dataframe.

How it is:

                      data      data1    data2
     0                apple     nan      nan
     1                nan       orange   nan
     2                nan       nan      Mango

How I want it to be:

                      data     data1   data2
     0                apple    orange  Mango

2 Answers

2
anky_91 On Best Solutions

Use:

df.apply(lambda x: pd.Series(x).dropna().values)

    data   data1  data2
0  apple  orange  Mango
1
Scott Boston On

Try using to_numpy or .values to clear indexing for alignment:

df.apply(lambda x: x.dropna().to_numpy())

OR

df.apply(lambda x: x.dropna().values)

Output:

    data   data1  data2
0  apple  orange  Mango