You could do
In [278]: df = pd.DataFrame([[1, np.nan], [2, np.nan], [np.nan, 3]]) In [279]: df Out[279]: 0 1 0 1 NaN 1 2 NaN 2 NaN 3 In [280]: df.sum(1) Out[280]: 0 1 1 2 2 3 dtype: float64
Since NaN are treated as 0 during summation, they are not displayed.
A few caveats: you must be sure that only one of the columns has a non-Nan for this. It will also work only with numeric data.
You can also use
df.fillna(method='ffill', axis=1).iloc[:, -1]
The last column will now contain all valid observations, since the actual ones were filled in front. See the documentation here . The second method should be more flexible, but slower. I cut off each row and last column with iloc[:, -1] .
source share