I am given a data set with many NaN values and I wanted to fill the null value with the mean of each column. So I tried the following code:
def fill_mean():
m = [df.columns.get_loc(c) for c in df.columns if c in missing]
for i in m:
df[df.columns[i]] =df[df.columns[i]].fillna(value=df[df.columns[i]].mean())
return df
but I get this error:
TypeError: must be str, not int
The columns I'm trying to fill are all composed by the same type: which is either 'float64' or 'O'.
I suspect the problem derives from this fact, but how can I solve it?
Edit: I created a dictionary containing the column which contains the index of the columns where some data are missing and each column's type.
di = dict(zip(missing, m2))
def fill_mean():
m = [df.columns.get_loc(c) for c in df.columns if c in missing]
for i in m:
if di[m] == "dtype('float64')":
df[df.columns[i]] = df[df.columns[i]].fillna(value=df[df.columns[i]].mean())
return df
If I run fill_mean(), now I get a different error:
if di[m] == "dtype('float64')":
TypeError: unhashable type: 'list'