1

I have the following dataframe.

ID   name   work   type
1    vir     NaN   NaN
1    NaN     QA     A
1    vir     NaN    A
2    Ru      NaN   NaN
2    NaN     QC    NaN
3    NaN     ER    NaN
3    Wish    NaN   NaN 
3    NaN     NaN   NaN
3    NaN     NaN    C

I want to fill in missed values based on ID and after filling value remove duplicates based on ID.

What is the best way to get the result of the dataframe below?

ID   name   work   type
1    vir     QA      A
2    Ru      QC
3    Wish    ER      C
3
  • 3
    df.groupby('ID').first() Commented Aug 18, 2022 at 4:01
  • Are the missing values empty strings or NaNs or something else? Commented Aug 18, 2022 at 4:01
  • Missing values are NaNs. Commented Aug 18, 2022 at 4:03

1 Answer 1

2

Use:

df.groupby('ID').first()
df = pd.DataFrame({
    'ID' : [1,1,1,2,2,3,3,3,3],
    'name' : ['vir' ,'', 'vir', 'Ru', '', '','Wish','',''],
    'work' : ['','QA', '', '', 'QC', 'ER', '','',''],
    'type' : ['','A','A', '','','','','','C']
})

df = df.replace('' ,None )

df.groupby('ID').first()

Output:

ID  name    work  type      
1   vir     QA    A
2   Ru      QC    None
3   Wish    ER    C
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.