Fill NaN using values from different rows which has same column 1 value

44 Views Asked by At

I have this df

dfo = pd.DataFrame([['aa', 'aa1', 1,np.NaN ,np.NaN ,np.NaN ,np.NaN ],
                    ['aa','aa1',np.NaN, np.NaN, 3,np.NaN , np.NaN],
                    ['aa','aa1',np.NaN,np.NaN ,np.NaN , 4,np.NaN ],
                    ['bb','bb1',np.NaN,7 ,np.NaN , np.NaN,np.NaN ],
                    ['bb','bb1',5,np.NaN ,np.NaN , np.NaN,np.NaN ],
                   ],
                   columns=['category','category1','A', 'B', 'C', 'D','E'])

original dataframe - something like this

and I would like to merge the rows by category so that corresponding NaNs are filled from other rows with the same category.

I need this:

dfr = pd.DataFrame([['aa','aa1',1,np.NaN ,3 ,4 ,np.NaN ],

                    ['bb','bb1',5,7 ,np.NaN , np.NaN,np.NaN ],
                   ],
                   columns=['category','category1','A', 'B', 'C', 'D','E'])

required dataframe - something like this

I tried using fillna() however i don't see any option to fill by category row.

0

There are 0 best solutions below