0

Here is a sample of a df I am working with. I am particularly interested in these two columns rusher and receiver.

     rusher     receiver
0   A.Ekeler      NaN
1   NaN        S.Barkley
2   C.Carson      NaN
3   J.Jacobs      NaN
4   NaN         K.Drake

I want to run a groupby that considers all of these names in both columns (because the same name can show up in both columns).

My idea is to create a new column player, and then I can just groupby player, if that makes sense. Here is what I want my output to look like

      rusher    receiver        player
0   A.Ekeler      NaN          A.Ekeler
1   NaN        S.Barkley       S.Barkley
2   C.Carson      NaN          C.Carson
3   J.Jacobs      NaN          J.Jacobs
4   NaN         K.Drake        K.Drake

I would like to take the name from whichever column it is listed under in that particular row and place it into the player column, so I can then run a groupby.

I have tried various string methods but I don't know how to work around the NaNs

1 Answer 1

1

Check with fillna

df['player'] = df['rusher'].fillna(df['receiver'])
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.