I have figured out how to fill the NaN values with the previous cell by using df.fillna(method='ffill')
.
However, I am not sure how to base it on a condition that if the country name differs from the country name in its previous cell, then the total case cell value should be 0, otherwise replace NaN
with the previous cell's total case value.
Simply using groupby
with fillna
will give the wanted result. columns
here are all the columns you want to apply the missing value logic to.
columns = ['total_cases', 'total_deaths', ...]
df[columns] = df.groupby('location')[columns].fillna(method='ffill').fillna(0)
Note that you need to apply fillna
twice, once with forward fill and once with a constant 0 to fill all nan values. This is to make sure that any nans that starts in a new group are filled with 0.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.