Home > front end >  Iterating through all values of df to replace zero values
Iterating through all values of df to replace zero values

Time:10-26

I want to iterate through all values of a df so that if it detects some zero, I can replace that specific element with the mean of the adjacent columns.

I tried this code but I dont know why isnt working. Somebody can help me, please??

import pandas as pd
import numpy as np
import numpy as np

d = {'col1': [4, 1, 2], 'col2': [1, 0, 4], 'col3': [12, -2, 4]}
df = pd.DataFrame(data=d)


for i in range(len(df)):
        for j in list(df):
                if df.loc[i,j] is np.nan:
                        df.loc[i,j] = (df.loc[i,j-1]   df.loc[i, j 1])/2

So that 0 in the position 2x2 of the matrix would be replace with the mean between 1 and -2 (the elements at the same line and adjacent columns)

CodePudding user response:

There's a couple of small things, but I hope this helps.

import pandas as pd
import numpy as np
d = {'col1': [4, 1, 2], 'col2': [1, np.nan, 4], 'col3': [12, -2, 4]}
df = pd.DataFrame(data=d)

rows, cols = df.shape
for i in range(rows):
        for j in range(cols):
                if np.isnan(df.iloc[i,j]):
                        df.iloc[i,j] = (df.iloc[i,j-1]   df.iloc[i, j 1])/2

CodePudding user response:

You have to replace list(df) by range(len(df.columns))

and df.loc by df.iloc

for i in range(len(df)):
        for j in range(len(df.columns)):
                if pd.isnull(df.iloc[i,j]):
                        df.iloc[i,j] = (df.iloc[i-1,j]   df.iloc[i 1, j])/2
  • Related