当我想将数字转换为分类时,以下代码不起作用
data_train['review_scores_rating'] = pd.DataFrame({'value': np.random.randint(20, 100, 1000)})
data_train['review_scores_rating'] = pd.cut(data_train['review_scores_rating'].value,
bins=[0, 50,80, np.inf],
labels=["Detractors", "Passives", "Promoters"])
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-49-ba9263d7208f> in <module>
1 data_train['review_scores_rating'] = pd.DataFrame({'value': np.random.randint(20, 100, 1000)})
----> 2 data_train['review_scores_rating'] = pd.cut(data_train['review_scores_rating'].value,
3 bins=[0, 50,80, np.inf],
4 labels=["Detractors", "Passives", "Promoters"])
/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
5065 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5066 return self[name]
-> 5067 return object.__getattribute__(self, name)
5068
5069 def __setattr__(self, name, value):
AttributeError: 'Series' object has no attribute 'value'
答案 0 :(得分:0)
我相信这一行:
data_train['review_scores_rating'] = pd.DataFrame({'value': np.random.randint(20, 100, 1000)})
没有做你想做的事。
print(type(pd.DataFrame({'value': np.random.randint(20, 100, 1000)})))
> output: <class 'pandas.core.frame.DataFrame'>
print(type(data_train['review_scores_rating']))
> output: <class 'pandas.core.series.Series'>
熊猫似乎将分配给review_scores_rating
的DataFrame转换为Series,这意味着列value
不再存在。我对熊猫还不够熟悉,无法说明为什么会这样,但是我认为不能将DataFrame分配给列是有道理的。
您提供的代码的预期输出是什么?您是否要生成多维DataFrame?