python - 如何对数据集表中的列索引进行标签编码?

标签 python pandas dataframe indexing

我正在尝试对第二列进行标签编码,但出现错误。我究竟做错了什么? 我能够对第一列进行编码

    data.head()
        area_type   availability    location    size    society total_sqft  bath    balcony price
    0   Super built-up Area 19-Dec  Electronic City Phase II    2 BHK   Coomee  1056    2.0 1.0 39.07
    1   Plot Area   Ready To Move   Chikka Tirupathi    4 Bedroom   Theanmp 2600    5.0 3.0 120.00
    2   Built-up Area   Ready To Move   Uttarahalli 3 BHK   NaN 1440    2.0 3.0 62.00
    3   Super built-up Area Ready To Move   Lingadheeranahalli  3 BHK   Soiewre 1521    3.0 1.0 95.00
    4   Super built-up Area Ready To Move   Kothanur    2 BHK   NaN 1200    2.0 1.0 51.00
enc = LabelEncoder()
data.iloc[:,2] = enc.fit_transform(data.iloc[:,2])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-53fda4a71b5e> in <module>()
      1 enc = LabelEncoder()
----> 2 data.iloc[:,2] = enc.fit_transform(data.iloc[:,2])

~/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/label.py in fit_transform(self, y)
    110         """
    111         y = column_or_1d(y, warn=True)
--> 112         self.classes_, y = np.unique(y, return_inverse=True)
    113         return y
    114 

~/anaconda3/lib/python3.6/site-packages/numpy/lib/arraysetops.py in unique(ar, return_index, return_inverse, return_counts, axis)
    208     ar = np.asanyarray(ar)
    209     if axis is None:
--> 210         return _unique1d(ar, return_index, return_inverse, return_counts)
    211     if not (-ar.ndim <= axis < ar.ndim):
    212         raise ValueError('Invalid axis kwarg specified for unique')

~/anaconda3/lib/python3.6/site-packages/numpy/lib/arraysetops.py in _unique1d(ar, return_index, return_inverse, return_counts)
    272 
    273     if optional_indices:
--> 274         perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
    275         aux = ar[perm]
    276     else:

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

我想对第二列“位置”进行标签编码,如果我使用 data.iloc[:,1] = enc.fit_transform(data.iloc[:,1]) 索引,我可以标签编码可用性列,所以 我该如何解决这个问题?

最佳答案

您的列的数据类型是什么?

出现错误的原因是标签编码器无法对数字(并且np.nan是 float )和字符串进行排序。

要解决此问题,您可以: - 将任何 nan 替换为空字符串 data['col_name'].fillna('',inplace=True); - 通过输入 data['col_name'] = data['col_name'].astype(str)

将列转换为字符串

关于python - 如何对数据集表中的列索引进行标签编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52232910/

相关文章:

python - 如何动态显示 tkinter 模块中的数据-Python

python - MechanicalSoup 棘手的 html 表格

Python Pandas 更新数据框并计算更新的单元格数量

python - 如果在 jupyter notebook 中使用 python 或 pandas,如何将列中的逗号分隔字符串拆分为不同的列

python - 如何将列值除以不同行的值?

python - 检查字符串中是否存在特定子字符串(存在于数据帧的行中)

python - 从两个数据框中获取匹配字符串的索引

Python 2.7 CSV文件读/写\xef\xbb\xbf代码

python - 将真实噪声添加到高斯分布,同时保持高于/低于阈值的样本数量大致恒定

r - 根据条件更改分组中的值