I have a dataframe with some missing values. I need to update one 'nan'
value with the last value not 'nan'
in a row.
Example:
nome
0 ESTAMPA DIGITAL 1
1 nan
2 nan
3 nan
4 nan
5 ESTAMPA DIGITAL 2
6 nan
7 nan
8 nan
9 nan
need to produce something like this:
nome
0 ESTAMPA DIGITAL 1
1 ESTAMPA DIGITAL 1
2 ESTAMPA DIGITAL 1
3 ESTAMPA DIGITAL 1
4 ESTAMPA DIGITAL 1
5 ESTAMPA DIGITAL 2
6 ESTAMPA DIGITAL 2
7 ESTAMPA DIGITAL 2
8 ESTAMPA DIGITAL 2
9 ESTAMPA DIGITAL 2
current code is:
for index, row in df_produtos.iterrows():
is_null = pd.isnull(row['nome'])
if is_null == False:
nome = row['nome']
print(nome)
elif is_null == True:
row['nome'] = nome
But it's not working. Somebody can help please?
PS: the print(nome)
is printing only "valid"
values that is not 'nan'
. But it's not setting the new value.
CodePudding user response:
Try using .ffill()
to forward fill the NaN
values, as follows:
df_produtos['nome'] = df_produtos['nome'].ffill()