Let's say I have a dataframe that looks like this
A B C
0 NaN NaN lizard
1 NaN penguin giraffe
2 horse turtle NaN
What I want to do is add a column (D) whose value is either that of column A if it exists otherwise it should be a combination of values in column B and C providing they are both not null/none. If column A is null and either of B or C is null, then D should be null/none.
I can do as such in regards to column A by using numpy's where function as such:
df['D'] = np.where(df['A'].notnull, df['A'], np.nan)
However I don't know how I would also add this column if both B and C aren't null/none.
Ideally the data frame would then look like this:
A B C D
0 NaN NaN lizard NaN
1 NaN penguin giraffe penguingiraffe
2 horse turtle NaN horse
np.whereapproach is fine if you replacenp.nanwithdf["B"]+df["C"].