0

I've a dataframe and some of the values are missing. Luckily there are other rows which share the same attributes and values in other columns.

df = pd.DataFrame({'A': ('x','x','y'),
                  'B': (2,2,2),
                  'C':(3, np.nan, 7)})
A B C
x 2 3
x 2 nan
y 2 7

Is there a fill method to populate the nan value in the 2nd row based on the common attributes of column A and B? i.e. nan should be filled to 3.

I've got a bigger dataframe so need a way of doing it systematically.

Andrew
  • 1
  • use `ffill()` method i.e `df['C']=df['C'].ffill()` – Anurag Dabas Mar 25 '21 at 05:11
  • no, that's not what i am after. It needs to reference column A and B (in practice, more columns). forward fill is not what i am after. I could technically have 2nd row and 3rd row swtiched and ffill will give a different result. – Andrew Mar 25 '21 at 05:24
  • then make use of `groupby()` method...see https://stackoverflow.com/questions/46391128/pandas-fillna-using-groupby – Anurag Dabas Mar 25 '21 at 05:25
  • yea that might work for me... – Andrew Mar 25 '21 at 05:29

0 Answers0