Search code examples
pythonpandasdataframenumpyfillna

How do I fill value in empty dataframe?


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

Solution

  • 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