英文:
Changing a Data Frame over a string in python
问题
我正在尝试使用以下代码更改数据框中的分类数据:
CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'embark_town', 'alone']
for i in CATEGORICAL_COLUMNS:
dfTrain[i] = pd.factorize(dfTrain[i])[0]
dfTrain.head()
但是我收到了以下错误信息:
'DataFrame' object has no attribute 'i'
我该如何修复这个问题?
英文:
I'm trying to change Categorical Data from my data frame using the code
CATEGORICAL_COLUMNS = ['sex','n_siblings_spouses', 'parch', 'class',
'embark_town', 'alone']
for i in CATEGORICAL_COLUMNS:
dfTrain[i] = pd.factorize(dfTrain.i)[0]
dfTrain.head()
But I get the error:
'DataFrame' object has no attribute 'i'
How would I fix this?
答案1
得分: 1
i
不是一个属性,你不能使用点表示法:
CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class',
'embark_town', 'alone']
for i in CATEGORICAL_COLUMNS:
dfTrain[i] = pd.factorize(dfTrain[i])[0] # .i -> [i]
更新
如果你使用sklearn
,你可以使用OrdinalEncoder
:
from sklearn.preprocessing import OrdinalEncoder
oe = OrdinalEncoder()
dfTrain[CATEGORICAL_COLUMNS] = oe.fit_transform(dfTrain[CATEGORICAL_COLUMNS])
然后使用transform_inverse
来解码数值。
英文:
i
is not an attribute, you can't use dot notation:
CATEGORICAL_COLUMNS = ['sex','n_siblings_spouses', 'parch', 'class',
'embark_town', 'alone']
for i in CATEGORICAL_COLUMNS:
dfTrain[i] = pd.factorize(dfTrain[i])[0] # .i -> [i]
Update
If you use sklearn
, you can use OrdinalEncoder
:
from sklearn.preprocessing import OrdinalEncoder
oe = OrdinalEncoder()
dfTrain[CATEGORICAL_COLUMNS] = oe.fit_transform(dfTrain[CATEGORICAL_COLUMNS])
and use transform_inverse
to decode numeric values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论