4

I have A dataframe as below:

Car SeriesNo White Red Black
Proton A111 2 
Proton A111 4
Proton A111 5

My Expected output will be:

Car SeriesNo White Red Black
Proton A111 2 4 5

Anyone have ideas on this?

asked Sep 2, 2019 at 4:47
1
  • There are multiple groups by first 2 columns? Commented Sep 2, 2019 at 4:51

1 Answer 1

3

Use GroupBy.first for get first non missing values per groups:

#if necessary replace empty strings to missing values
#df = df.replace('',np.nan)
df = df.groupby(['Car','SeriesNo'], as_index=False).first()
print (df)
 Car SeriesNo White Red Black
0 Proton A111 2.0 4.0 5.0
answered Sep 2, 2019 at 4:50
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting use of groupby here. Nice answer as always, much better than mine :)
@Chris - It depends what OP need, but I guess there are multiple groups, so then groupby here is necessary.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.