Search code examples
pythonpandascoalescefillnanull-coalescing-operator

How to conditional left shift based on null values in Pandas


I have a data frame like

df = pd.DataFrame({"A":[1,np.nan,5],"B":[np.nan,10,np.nan], "C":[2,3,np.nan]})
     A     B   C
0    1   NaN   5
1   NaN  10   NaN
2    2    3   NaN 

I want to left shift all the values to occupy the nulls. Desired output:

     A     B   C 
0    1     5  NaN
1   10   NaN  NaN
2    2    3   NaN 

I tried doing this using a series of df['A'].fillna(df['B'].fillna(df['C']) but in my actual data there are more than 100 columns. Is there a better way to do this?


Solution

  • Let us do

    out = df.T.apply(lambda x : sorted(x,key=pd.isnull)).T
    Out[41]: 
          A    B   C
    0   1.0  5.0 NaN
    1  10.0  NaN NaN
    2   2.0  3.0 NaN