数据具有以下属性
这是我所做的
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)值,实际上是一个ndarray的int64值。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)