I have a dataframe in which the weight of each client is not well kept resulting in:
| CLIENT_ID | ENCOUNTER_DATE | WEIGHT_KG |
|---|---|---|
| 16081 | 2018-12-17 | 70.0 |
| 16081 | 2019-03-19 | 0.0 |
| 16081 | 2019-04-18 | 0.0 |
| 16081 | 2019-06-07 | 0.0 |
| 20011 | 2020-02-27 | 0.0 |
| 20011 | 2020-03-27 | 0.0 |
| 20011 | 2020-04-27 | 57.0 |
| 20011 | 2020-06-07 | 0.0 |
| 20011 | 2020-07-07 | 60.0 |
| 20020 | 2020-01-01 | 0.0 |
The table is sorted by CLIENT_ID and DATE_ENCOUNTER.
How can I replace the values for each CLIENT_ID where the WEIGHT_KG = 0.0 by first forward filling the entries, then back filling them? (Depending on the entries for each CLIENT_ID This would result in a dataframe shown below:
| CLIENT_ID | ENCOUNTER_DATE | WEIGHT_KG |
|---|---|---|
| 16081 | 2018-12-17 | 70.0 |
| 16081 | 2019-03-19 | 70.0 |
| 16081 | 2019-04-18 | 70.0 |
| 16081 | 2019-06-07 | 70.0 |
| 20011 | 2020-02-27 | 57.0 |
| 20011 | 2020-03-27 | 57.0 |
| 20011 | 2020-04-27 | 57.0 |
| 20011 | 2020-06-07 | 57.0 |
| 20011 | 2020-07-07 | 60.0 |
| 20020 | 2020-01-01 | 0.0 |
Here is code to generate the df:
df = pd.DataFrame({"CLIENT_ID": [16081, 16081, 16081, 16081, 20011, 20011, 20011, 20011, 20011,20020],
"ENCOUNTER_DATE": ['2018-12-17', '2019-03-19', '2019-04-18', '2019-06-07', '2020-02-27', '2020-03-27', '2020-04-27', '2020-06-07', '2020-07-07','2020-01-01'],
"WEIGHT_KG": [70, 0, 0, 0, 0, 0, 57, 0, 60,0]})