'Drop nan of each column in Pandas DataFrame

I have a dataframe as example:

     A     B     C
0    1
1    1
2    1
3    1     2
4    1     2
5    1     2
6          2     3
7          2     3
8          2     3
9                3
10               3
11               3

And I would like to remove nan values of each column to get the result:

     A     B     C
0    1     2     3
1    1     2     3
2    1     2     3
3    1     2     3
4    1     2     3
5    1     2     3

Do I have an easy way to do that?



Solution 1:[1]

Given

>>> df 
      A    B    C
0   1.0  NaN  NaN
1   1.0  NaN  NaN
2   1.0  NaN  NaN
3   1.0  2.0  NaN
4   1.0  2.0  NaN
5   1.0  2.0  NaN
6   NaN  2.0  3.0
7   NaN  2.0  3.0
8   NaN  2.0  3.0
9   NaN  NaN  3.0
10  NaN  NaN  3.0
11  NaN  NaN  3.0

use

>>> df.apply(lambda s: s.dropna().to_numpy()) 
     A    B    C
0  1.0  2.0  3.0
1  1.0  2.0  3.0
2  1.0  2.0  3.0
3  1.0  2.0  3.0
4  1.0  2.0  3.0
5  1.0  2.0  3.0

Solution 2:[2]

You can apply a custom sorting function for each column that doesn't actually sort numerically, it justs moves all the NaN values to the end of the column. Then, dropna:

df = df.apply(lambda x: sorted(x, key=lambda v: isinstance(v, float) and np.isnan(v))).dropna()

Output:

>>> df
     A    B    C
0  1.0  2.0  3.0
1  1.0  2.0  3.0
2  1.0  2.0  3.0
3  1.0  2.0  3.0
4  1.0  2.0  3.0
5  1.0  2.0  3.0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 richardec