I have a data frame that looks something like this: (there are about 100 more columns irrelevant to my conditional column calculation)
col1 col2 col3
a NaN NaN
b NaN NaN
NaN a NaN
NaN b NaN
NaN NaN a
NaN NaN b
I need to add a column to put those values together so that it looks like this:
col1 col2 col3 col4
a NaN NaN a
b NaN NaN b
NaN a NaN a
NaN b NaN b
NaN NaN a a
NaN NaN b b
I'm trying to use something like this (which has worked for other conditions, such as searching for specific strings):
df['col4'] = [x if (~pd.isnull(x)) else y if (~pd.isnull(y)) else z if (~pd.isnull(z)) else '' for x,y,z in zip(df['col1'], df['col2'], df['col3])
However, this only performs the first test condition and sets the rest as NaN, even if I set the else condition to set the rest as empty strings. It looks like this:
col1 col2 col3 col4
a NaN NaN a
b NaN NaN b
NaN a NaN NaN
NaN b NaN NaN
NaN NaN a NaN
NaN NaN b NaN
Could anyone help explain why this isn't working (and what these kinds of "functions" are called?)
Edit: to clarify, there are other columns as well, but I'm not concerned about their values in the calculation for 'col4'