Example of data that I want to replace
数据具有以下属性
- 购买 V-高、高、中、低
- 维持 V-高、高、中、低
- 门 2、3、4、5-更多
- 2,4人以上
- lug_boot 小、中、大
- 安全性低,中高
这就是我所做的
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())
打印时显示:
Cannot compare types 'ndarray(dtype=int64)' and 'str'
你们中的一些人string
你通过替换为 (int
)值,实际上是一个ndarray
of int64
价值观。
你只有int64( here actually ndarray(dtype=int64))
在此列中键入数据。
参见文档pandas.Dataframe.replace() https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.replace.html.
replace()
尝试寻找并与它们进行比较str
您传递的值。
df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})
找到所有"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)
Out:
TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)