1
$\begingroup$

Hopefully, you don't mind me posting a question here instead of the regular stack exchange forum. I'm learning ML basics and practising pandas.

assuming the data frame is called df, column name = column name

Suppose I want to replace some 'dirty' values in the column 'column name'. There are "not known" values in this column that mean nothing so i would like to replace them with the mode.

df['columnname'].mode()

returns

0   dog
dtype: object

this code below replaces the "not known" values as NaN rather than the mode.

df.loc[df.index[df['columnname'] == "not known"].tolist(),'columnname'] = df['columnname'].mode()

what am i missing? or how should i fix the code?

$\endgroup$

2 Answers 2

1
$\begingroup$

Your entire code is correct except at the last point where you are equating with df['columnname'].mode(). The value here should have a dtype int or string but this has a dtype object. Just replace it with df['columnname'].mode().values and you are good to go.

Also, I see a lot of stuff that is not required here. Since you are using pd.loc, you can simply write:

df.loc[df['columnname'] == "not known",'columnname'] = df['columnname'].mode().values
$\endgroup$
0
$\begingroup$

Try this

df['columnname'].replace({'not known' : df['columnname'].mode().values} , inplace=True)

This is the most direct and intuitive way of replacing values given you already know what the "dirty" value is.

$\endgroup$
1
  • $\begingroup$ Any chance you could explain how or why that should be tried? $\endgroup$
    – Stephen Rauch
    Nov 11, 2019 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.