Pandas DataFrame 似乎没有 "factorize" 方法
Pandas DataFrame seems not to have "factorize" method
我目前正在学习
的机器学习 jupyter 课程
https://github.com/ageron/handson-ml
在 chapter02 中,Pandas DataFrame "housing_cat" 应该被 pandas 方法分解 "factorize"
housing_cat.factorize()
但是,python告诉我
'DataFrame' object has no attribute 'factorize'
当我键入 "housing_cat." 并使用 TAB 键选择方法时,我也找不到 "factorize"。 Pandas 由 "import pandas" 导入,type(housing_cat) 也告诉我它是一个 pandas 数据框。我使用 pandas v0.20.3
那么这里可能是什么问题?
pandas 0.20.3 肯定有 .factorize() 但它没有附加到数据帧。
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.factorize.html
(注意 url 中的 pandas.factorize.html)
而是用 pandas.factorize() 或 pd.factorize()
调用
有两种方法。
df.apply(lambda x : pd.factorize(x)[0])
或
df.apply(lambda x : x.astype('category').cat.codes)
为什么会导致您的问题:执行以下操作即可
from pandas import factorize
housing_cat.factorize()
在应用 housing_cat.factorize() 之前,您可能需要确保 housing_cat 是 Series 类型而不是 DataFrame。
我目前正在学习
的机器学习 jupyter 课程https://github.com/ageron/handson-ml
在 chapter02 中,Pandas DataFrame "housing_cat" 应该被 pandas 方法分解 "factorize"
housing_cat.factorize()
但是,python告诉我
'DataFrame' object has no attribute 'factorize'
当我键入 "housing_cat." 并使用 TAB 键选择方法时,我也找不到 "factorize"。 Pandas 由 "import pandas" 导入,type(housing_cat) 也告诉我它是一个 pandas 数据框。我使用 pandas v0.20.3 那么这里可能是什么问题?
pandas 0.20.3 肯定有 .factorize() 但它没有附加到数据帧。 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.factorize.html
(注意 url 中的 pandas.factorize.html)
而是用 pandas.factorize() 或 pd.factorize()
调用有两种方法。
df.apply(lambda x : pd.factorize(x)[0])
或
df.apply(lambda x : x.astype('category').cat.codes)
为什么会导致您的问题:执行以下操作即可
from pandas import factorize
housing_cat.factorize()
在应用 housing_cat.factorize() 之前,您可能需要确保 housing_cat 是 Series 类型而不是 DataFrame。