Python 未编码的列

Python 未编码的列,python,encoder,Python,Encoder,我有一个名为“教育”的专栏。我试图使用标签编码器对其进行编码,但出现以下错误 [我无法发布图像,因此数据链接附在下面][1] TypeError Traceback (most recent call last) /opt/conda/lib/python3.7/site-packages/sklearn/preprocessing/_label.py in _encode(values, uniques, encode, chec

我有一个名为“教育”的专栏。我试图使用标签编码器对其进行编码,但出现以下错误

[我无法发布图像,因此数据链接附在下面][1]

TypeError                                 Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/sklearn/preprocessing/_label.py in _encode(values, uniques, encode, check_unknown)
    112         try:
--> 113             res = _encode_python(values, uniques, encode)
    114         except TypeError:

/opt/conda/lib/python3.7/site-packages/sklearn/preprocessing/_label.py in _encode_python(values, uniques, encode)
     60     if uniques is None:
---> 61         uniques = sorted(set(values))
     62         uniques = np.array(uniques, dtype=values.dtype)

TypeError: '<' not supported between instances of 'str' and 'float'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-90-64573b8efccb> in <module>
----> 1 df_temp['education']=encoder1.fit_transform(df_temp['education'])

/opt/conda/lib/python3.7/site-packages/sklearn/preprocessing/_label.py in fit_transform(self, y)
    254         """
    255         y = column_or_1d(y, warn=True)
--> 256         self.classes_, y = _encode(y, encode=True)
    257         return y
    258 

/opt/conda/lib/python3.7/site-packages/sklearn/preprocessing/_label.py in _encode(values, uniques, encode, check_unknown)
    115             types = sorted(t.__qualname__
    116                            for t in set(type(v) for v in values))
--> 117             raise TypeError("Encoders require their input to be uniformly "
    118                             f"strings or numbers. Got {types}")
    119         return res

TypeError: Encoders require their input to be uniformly strings or numbers. Got ['float', 'str']  ```


  


  [1]: https://i.stack.imgur.com/5F2uC.png
TypeError回溯(最近一次调用)
/opt/conda/lib/python3.7/site-packages/sklearn/preprocessing//u label.py in\u encode(值、唯一性、编码、检查未知)
112尝试:
-->113 res=_encode_python(值、唯一性、编码)
114除类型错误外:
/opt/conda/lib/python3.7/site-packages/sklearn/preprocessing//u label.py in\u encode\u python(值、唯一性、编码)
60如果uniques为无:
--->61唯一性=已排序(设置(值))
62 uniques=np.array(uniques,dtype=values.dtype)
TypeError:“您可以试试这个


df_temp['education']=encoder1.fit_transform(df_temp['education'].astype(str))


错误消息中有什么不清楚?您的输入中似乎有混合类型,但需要另一种类型。@pavel我对这一点不熟悉,似乎无法理解。该列有一些nan值,我用一些随机数替换了它们。我该如何解决这个问题?这非常有效。如果您不介意,可以解释一下为什么添加“.astype(str)”有效吗?这可能是因为数据的读取方式,即数字被读取为浮点,文本被读取为字符串,或者数据类型被读取为浮点,并在
fillna
操作后更改。我想发生的是,u有一个包含字符串值的列,u用整数填充。。使列同时具有字符串和整数。在上面的答案中,不管现有类型如何,所有内容都转换为字符串并进行编码。若我所说的是对的,那个么你们不能用整数填充字符串列的na