The fillna can’t be so flexible as to create a rule like yours, in which case I would use the (iterrows) to go through the dataframe to make the adjustments.
Following your example I created the following dataframe:
And to fill in the empty fields I used the following code:
import numpy as np
for index, row in df.iterrows():
if index > 1:
if pd.isnull(row['QTDVENDADIARIA']):
df.loc[index, 'QTDVENDADIARIA'] = (df.loc[index-1,'QTDVENDADIARIA']+df.loc[index-2, 'QTDVENDADIARIA'])/2
if pd.isnull(row['PRECOMEDIODIARIO']):
df.loc[index, 'PRECOMEDIODIARIO'] = (df.loc[index-1,'PRECOMEDIODIARIO']+df.loc[index-2,'PRECOMEDIODIARIO'])/2
if pd.isnull(row['EMISSAO']):
df.loc[index, 'EMISSAO'] = row['SEQ_DATA']
else:
if pd.isnull(row['QTDVENDADIARIA']):
df.loc[index, 'QTDVENDADIARIA'] = (df.loc[index+2,'QTDVENDADIARIA']+df.loc[index+1, 'QTDVENDADIARIA'])/2
if pd.isnull(row['PRECOMEDIODIARIO']):
df.loc[index, 'PRECOMEDIODIARIO'] = (df.loc[index+2,'PRECOMEDIODIARIO']+df.loc[index+1,'PRECOMEDIODIARIO'])/2
Explaining a little code...
The iterrows function runs through all the lines of the dataframe, I do this to know where I have empty fields.
Speaking only of the value fields, if the field is empty I do the average account based on the indexes of the previous lines (-1 and -2).
When the date field is empty I simply copy the data from the column ('SEQ_DATA')
The initial IF is used to check which index the routine is, because, if it was in the index 0 or 1, when trying to do the calculation would give error ( I did this based on the response of your comment and assuming the same premise that used in the -0 to 1 also).
Once that’s done, that’s the result:
If on the line whose
index
is equal to zeroQTDVENDADIARIA
be equalnan
what will replace you ?– Augusto Vasques
I would substitute the average of the next two
– Core