How can I change a cell based in a logic operator in Pandas?

I am using this dataset to get the % of people vaccinated. The problems is that there is an error in the dataset and the % of people vaccinated drop to zero in some days. I am trying to fix this error picking the previous value. But I still can't change the dataframe.

for x in list(range(0,len(brazil_data_covid.index))):
    if brazil_data_covid.iloc[x]['date'] > datetime.date(2021, 4, 1) and brazil_data_covid.iloc[x]['people_vaccinated_per_hundred'] == 0:
      brazil_data_covid.iloc[x]['people_vaccinated_per_hundred'] = brazil_data_covid.iloc[x-1]['people_vaccinated_per_hundred']

How can I tackle this?

enter image description here


Solution 1:

You could naively just replace the zeros:

brazil_data_covid['people_vaccinated_per_hundred'].replace(0, method='ffill', inplace=True)

Though this might easily replace actual zeros in the beginning of the series. One approach is to only ffill zeros that came from a non-zero value, i.e.:

invalid = brazil_data_covid['people_vaccinated_per_hundred'].pct_change() == -1
brazil_data_covid['people_vaccinated_per_hundred'] = \ 
    brazil_data_covid['people_vaccinated_per_hundred'].mask(invalid).ffill()

Then loop as you would normally.