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.

4
  • use ffill() method i.e df['C']=df['C'].ffill() Commented Mar 25, 2021 at 5: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. Commented Mar 25, 2021 at 5:24
  • then make use of groupby() method...see stackoverflow.com/questions/46391128/… Commented Mar 25, 2021 at 5:25
  • yea that might work for me... Commented Mar 25, 2021 at 5:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.