mardi 26 octobre 2021

handling missing data with average of 2 next and previous data in float type

I have a soil dataset that has few instances and a lot of missing data so I want to fill my missing data with the average of 2 values before and after my missing value. the data set is: [the picture of my data set][1] I have changed my missing values in the 'ph' column by 'A' and made a for-loop by if conditions that find 'A' values and replace them by the average the code is below:

    a1=df['ph'][i-2]
    a2=df['ph'][i-1]
    a3=df['ph'][i+1]
    a4=df['ph'][i+2]
    if df['ph'][i]==95:
        if (a1 and a2)and(a3 and a4)!=95:
            df['ph'][i]=(df['ph'][i-2]+df['ph'][i-1]+df['ph'][i+1]+df['ph'][i+2])/4
        else:
            df['ph'][i]=7.0808
    else:
        print('no')

when I run it returns me lots of no please help me to fix it

Aucun commentaire:

Enregistrer un commentaire