0

I have a dataframe as below:

name    category
----    ----
C58     UP 
D96     DOWN
C58     null

There are lots of null values in category column while they can easily be replaced using other rows for example we know C58 is UP so the null value shoud be UP.

1 Answer 1

1

IIUC, you want to fill the missing values in category by the values elsewhere in the same column with the same "name". Then you could use groupby + ffill:

df['category'] = df.groupby('name')['category'].ffill()

If each name has exactly one category value, you could also use groupby + transform('first'):

df['category'] = df.groupby('name')['category'].transform('first')

Output:

  name category
0  C58       UP
1  D96     DOWN
2  C58       UP
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.