Forgive me if the title a little bit confusing.
Assuming I have
test.h5
df.read_hdf('test.h5', 'testdata')
0 1 2 3 4 5 6
0 123 444 111 321 NaN NaN NaN
1 12 234 113 67 21 32 900
3 212 112 543 321 45 NaN NaN
0 321
1 900
2 45
0 1 2 3 4 5 6
0 123 444 111
1 12 234 113 67 21 32
3 212 112 543 321
df.iloc[:, :-1]
df.iloc[:, -1]
0 1 2 3 4 5
0 123 444 111 321 NaN NaN
1 12 234 113 67 21 32
3 212 112 543 321 45 NaN
0 NaN
1 900
2 Nan
You can use sorted to satisfy your condition i.e
ndf = df.apply(lambda x : sorted(x,key=pd.notnull),1)
This will give
0 1 2 3 4 5 6 0 NaN NaN NaN 123.0 444.0 111.0 321.0 1 12.0 234.0 113.0 67.0 21.0 32.0 900.0 3 NaN NaN 212.0 112.0 543.0 321.0 45.0
Now you can select the last column i.e
ndf.iloc[:,-1]
0 321.0 1 900.0 3 45.0 Name: 6, dtype: float64
ndf.iloc[:,:-1].apply(lambda x : sorted(x,key=pd.isnull),1)
0 1 2 3 4 5 0 123.0 444.0 111.0 NaN NaN NaN 1 12.0 234.0 113.0 67.0 21.0 32.0 3 212.0 112.0 543.0 321.0 NaN NaN