Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot do inplace boolean setting on mixed-types with a non np.nan value

Tags:

python

pandas

I am getting the error TypeError: Cannot do inplace boolean setting on mixed-types with a non np.nan value when I try to replace numeric values in multiple columns by a specific string value.

df =

TYPE  VD_1   VD_2    VD_3
AAA   1234   22122   2345
AAA   1234   2345    22122

This is how I do it:

df[df.isin([22122])] = "English"

or

df[df==22122] = "English"
like image 866
Dinosaurius Avatar asked Feb 16 '17 14:02

Dinosaurius


1 Answers

If you stack the df, then you can compare the entire df against the scalar value, replace and then unstack:

In [122]:
stack = df.stack()
stack[ stack == 22122] = 'English'
stack.unstack()

Out[122]:
  TYPE  VD_1     VD_2     VD_3
0  AAA  1234  English     2345
1  AAA  1234     2345  English

or replace:

In [125]:
df.replace(22122,'English', inplace=True)
df

Out[125]:
  TYPE  VD_1     VD_2     VD_3
0  AAA  1234  English     2345
1  AAA  1234     2345  English
like image 65
EdChum Avatar answered Sep 16 '22 12:09

EdChum