Issue
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.
Solution
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
Answered By - enke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.