how do i merge multiple columns containing numeric data in pandas but ignore empty cells

I think it is what you want.

import pandas as pd
df = pd.DataFrame({"A":[np.nan, 1, np.nan, np.nan, np.nan],
                   "B": [5, np.nan, 5, np.nan, np.nan]})
df['Z'] = df.sum(axis = 1)

Alternatively, you can use

df['Z'] = df.max(axis = 1)

Which might be safer if (per chance) you have multiple non-NULL values and just want one of them (the largest one in this case).

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top