python - Loop for imputation -


i make imutation single variable & return same variable

x = pd.dataframe(df, columns=['a']) imp = imputer(missing_values='nan', strategy='median', axis=0) x = imp.fit_transform(x) df['a'] = x 

however have many variables & want use loop this

f = df[[a, b, c, d, e]] k in f:     x = pd.dataframe(df, columns=k)     imp = imputer(missing_values='nan', strategy='median', axis=0)     x = imp.fit_transform(x)     df.k = x 

but:

typeerror: index(...) must called collection of kind, 'a' passed 

how can use loop imputation & return variables in dataframe?

a dataframe iterates on it's columns names k == 'a' in instance rather first column. implement with

f = df[[a, b, c, d, e]] k in f:     x = df[k]     imp = imputer(missing_values='nan', strategy='median', axis=0)     x = imp.fit_transform(x)     df[k] = x 

but want build new dataframe using apply column wise. like

df = df.apply(imp.fit_transform, raw=true, broadcast=true) 

or pandas has it's own methods working missing data: http://pandas.pydata.org/pandas-docs/stable/missing_data.html#filling-with-a-pandasobject


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -