1

I make an imutation for a single variable & return it to the 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 I have many variables & want to use loop like this

f = df[[a, b, c, d, e]] for 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 be called with a collection of some kind, 'a' was passed 

How can I use loop for imputation & return variables in dataframe?

2 Answers 2

1

A DataFrame iterates over it's columns names so k == 'a' in this instance rather than the first column. You could implement it with

f = df[[a, b, c, d, e]] for k in f: X = df[k] imp = Imputer(missing_values='NaN', strategy='median', axis=0) X = imp.fit_transform(X) df[k] = X 

But you probably just want to build a new dataframe using apply column wise. Something like

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

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

Sign up to request clarification or add additional context in comments.

Comments

1
for 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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.