Previous NaN Values
Given a dataframe with three columns:
- client_id
- ranking
- value
Write a function to fill the NaN values in the value column with the previous non-NaN value from the same client_id ranked in ascending order.
If there doesn’t exist a previous client_id then return the previous value.
Input:
print(clients_df)
client_id | ranking | value |
---|---|---|
1001 | 1 | 1000 |
1001 | 2 | NaN |
1001 | 3 | 1200 |
1002 | 1 | 1500 |
1002 | 2 | 1250 |
1002 | 3 | NaN |
1003 | 1 | 1100 |
1003 | 2 | NaN |
Output:
client_id | ranking | value |
---|---|---|
1001 | 1 | 1000 |
1002 | 1 | 1500 |
1003 | 1 | 1100 |
1001 | 2 | 1000 |
1002 | 2 | 1250 |
1003 | 2 | 1100 |
1001 | 3 | 1200 |
1002 | 3 | 1250 |
.....
Loading editor