1

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
1
  • Your np.where approach is fine if you replace np.nan with df["B"]+df["C"]. Commented Oct 27, 2021 at 14:31

1 Answer 1

2

Concat the columns B and C, then fill the NaN values in column A and assign this result to column D

df['D'] = df['A'].fillna(df['B'] + df['C'])

       A        B        C               D
0    NaN      NaN   lizard             NaN
1    NaN  penguin  giraffe  penguingiraffe
2  horse   turtle      NaN           horse
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.