Although @thesilkworkm beat me in the curb, it may be useful to know why exactly your own code doesn't work.
So, apart from the reshape issue, there are two more mistakes in your code; the first is that you erroneously ask for axis=1
in your imputer, while you should ask for axis=0
(which is the default value, and that's why it works when omitted completely, as in @thesilkworkm'a answer); from the docs:
axis : integer, optional (default=0)
The axis along which to impute.
- If axis=0, then impute along columns.
- If axis=1, then impute along rows.
The second mistake is your missing_values
argument, which should be 'NaN'
, and not 'nan'
; from the docs again:
missing_values : integer or “NaN”, optional (default=”NaN”)
The placeholder for the missing values. All occurrences of missing_values will be imputed. For missing values encoded as np.nan,
use the string value “NaN”.
So, just for offering an alternative but equivalent solution (beyond the one already provided by @thesilkworm), you can also fit & transform in one line:
imp = Imputer(missing_values ="NaN",strategy = "mean",axis = 0)
x['Age'] = imp.fit_transform(x['Age'].reshape(-1,1))