user39039
user39039

Reputation: 103

Python indexing from dataframe with iloc

I want to do something very easy, but I am new to Python and tried a lot of things, but it does not work. Say that I have the following dataframe:

df = pandas.DataFrame([10, 20, 0, 30, 50], columns = [‘Values’]

The objective is to write a function that replaces 0 by the average of the pre and consecutive elemente, that is, 0 <- (20+30)/2 = 25. Later on I want to use this function together with apply to apply it to every column of a much bigger dataFrame.

I did the following:

def avg_zeros(x):
    index = x[x == 0]
    x.loc[index+1, 0] = (0.5)*(x.loc[index, 0] + x.loc[index+2, 0]
    return(x)

However it keeps returning errors. I think the problem is with index = x[x==0],as it does not allow me to extract the index as an 'integer', but I tried different things and it does not seem to work.

Any tips? Thanks

Upvotes: 0

Views: 168

Answers (1)

KRKirov
KRKirov

Reputation: 4004

import numpy as np
import pandas as pd

df = pd.DataFrame([10, 20, 0, 30, 50], columns = ['Values'])

df.Values[df['Values'] == 0] = np.nan

df['Values'] = df['Values'].interpolate(how='linear')

print(df)

   Values
0    10.0
1    20.0
2    25.0
3    30.0
4    50.0

Upvotes: 2

Related Questions