无法比较类型“ndarray(dtype=int64)”和“str”

Mit*_*esh 11 python numpy

我要替换的数据示例 在此处输入图片说明

数据具有以下属性

  1. 购买 v 高、高、中、低
  2. 维持 v-高、高、中、低
  3. 门 2,3,4,5-more
  4. 2,4 人以上
  5. lug_boot 小、中、大
  6. 安全低、中、高

这是我所做的

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())
Run Code Online (Sandbox Code Playgroud)

打印时显示:

Cannot compare types 'ndarray(dtype=int64)' and 'str'
Run Code Online (Sandbox Code Playgroud)

Gee*_*ode 10

你们有些人string你通过与(取代int)值,实际上是一个ndarrayint64值。int64( here actually ndarray(dtype=int64))此列中只有类型数据。请参阅文件pandas.Dataframe.replace()replace()尝试寻找它们并将它们与str您传递的值进行比较。

df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})
Run Code Online (Sandbox Code Playgroud)

查找所有"vhigh"值并与当前包含的值进行比较,将其替换为4. 在比较时它失败了,因为尝试将str数据与int64 ('ndarray(dtype=int64)')

一个简单的例子来模拟这个:

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)
Run Code Online (Sandbox Code Playgroud)

出去:

TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'
Run Code Online (Sandbox Code Playgroud)