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

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'

Tags:

Python

Numpy