Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot compare types 'ndarray(dtype=int64)' and 'str'

Tags:

python

numpy

Example of data that I want to replace enter image description here

Data has the following attributes

  1. Buying v-high,high,med,low
  2. Maint v-high,high,med,low
  3. Doors 2,3,4,5-more
  4. persons 2,4-more
  5. lug_boot small,med,big
  6. safety low,med.high

here is what I did

enter code here
#Buying price generalization 
df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})
df["Buying_Price"]=df["Buying_Price"].replace({"high":3})
df["Buying_Price"]=df["Buying_Price"].replace({"med":2})
df["Buying_Price"]=df["Buying_Price"].replace({"low":1})

#Maintanace generalization 
df["Maintanance_price"]=df["Maintanance_price"].replace({"vhigh":4}) 
df["Maintanance_price"]=df["Maintanance_price"].replace({"high":3})   
df["Maintanance_price"]=df["Maintanance_price"].replace({"med":2})
df["Maintanance_price"]=df["Maintanance_price"].replace({"low":1})

#lug_boot generalization 
df["Lug_boot"]=df["Lug_boot"].replace({"small":1})
df["Lug_boot"]=df["Lug_boot"].replace({"med":2})
df["Lug_boot"]=df["Lug_boot"].replace({"big":3})

#Safety Generalization 
df["Safety"]=df["Safety"].replace({"low":1})
df["Safety"]=df["Safety"].replace({"med":2})
df["Safety"]=df["Safety"].replace({"big":3})

print(df.head())

while printing it showed:

Cannot compare types 'ndarray(dtype=int64)' and 'str'
like image 475
Mitesh Avatar asked Nov 26 '18 10:11

Mitesh


3 Answers

Some of you string you passed to replace with an (int)value, actually is an ndarray of int64 values. You only have int64( here actually ndarray(dtype=int64)) type data in this column. See document pandas.Dataframe.replace(). replace() try to seek and compare them with the str values you passed.

df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})

find all "vhigh" value and compare with the value currently contains, the replace it with 4. At the comparing it fails as try to compare str data with int64 ('ndarray(dtype=int64)')

A brief example to simulate this:

import pandas as pd
import numpy as np

a = np.array([1])
df = pd.DataFrame({"Maintanance_price": a})
df["Maintanance_price"] = df["Maintanance_price"].replace({"a":1})

print(df)

Out:

TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'
like image 171
Geeocode Avatar answered Oct 13 '22 20:10

Geeocode


I was facing the same issue and what worked for me was converting the datatype of the feature to an object type.

   train['Some_feature']=train.Some_feature.astype(object)

Hope it helps.

like image 2
Nitin Kumar Avatar answered Oct 13 '22 21:10

Nitin Kumar


You could try the following code:

df['Maintanance_price'].replace(to_replace = ['low', 'med','high','vhigh'], value =[1,2,3,4], inplace=True)
df.head()

Also, as suggested by @ouiemboughrra, check if the values have already been converted to numeric, in case you have rerun the cell.

like image 1
Ruby Avatar answered Oct 13 '22 21:10

Ruby