Sklearn 将字符串类标签更改为 int

Luk*_*asz 7 python pandas scikit-learn

我有一个 Pandas 数据框,我正在尝试将给定列中由字符串表示的值更改为整数。例如:

df = index    fruit   quantity   price 
         0    apple          5    0.99
         1    apple          2    0.99
         2   orange          4    0.89
         4   banana          1    1.64
       ...
     10023     kiwi         10    0.92
Run Code Online (Sandbox Code Playgroud)

我想看看:

df = index    fruit   quantity   price 
         0        1          5    0.99
         1        1          2    0.99
         2        2          4    0.89
         4        3          1    1.64
       ...
     10023        5         10    0.92
Run Code Online (Sandbox Code Playgroud)

我可以使用

df["fruit"] = df["fruit"].map({"apple": 1, "orange": 2,...})
Run Code Online (Sandbox Code Playgroud)

如果我有一个小列表要更改,这会起作用,但我正在查看具有 500 多个不同标签的列。有什么办法可以将其从 astring更改为 anint吗?

jez*_*ael 12

使用factorize然后categorical在必要时转换为:

df.fruit = pd.factorize(df.fruit)[0]
print (df)
   fruit  quantity  price
0      0         5   0.99
1      0         2   0.99
2      1         4   0.89
3      2         1   1.64
4      3        10   0.92
Run Code Online (Sandbox Code Playgroud)
df.fruit = pd.Categorical(pd.factorize(df.fruit)[0])
print (df)
  fruit  quantity  price
0     0         5   0.99
1     0         2   0.99
2     1         4   0.89
3     2         1   1.64
4     3        10   0.92

print (df.dtypes)
fruit       category
quantity       int64
price        float64
dtype: object
Run Code Online (Sandbox Code Playgroud)

此外,如果需要从1

df.fruit = pd.Categorical(pd.factorize(df.fruit)[0] + 1)
print (df)
  fruit  quantity  price
0     1         5   0.99
1     1         2   0.99
2     2         4   0.89
3     3         1   1.64
4     4        10   0.92
Run Code Online (Sandbox Code Playgroud)

  • 分类根据定义因式分解;没有理由直接做 (4认同)

小智 8

您可以使用sklearn.preprocessing

from sklearn import preprocessing

le = preprocessing.LabelEncoder()
le.fit(df.fruit)
df['categorical_label'] = le.transform(df.fruit)

Run Code Online (Sandbox Code Playgroud)

将标签转换回原始编码。

le.inverse_transform(df['categorical_label'])
Run Code Online (Sandbox Code Playgroud)


Max*_*axU 5

您可以使用分解方法:

In [13]: df['fruit'] = pd.factorize(df['fruit'])[0].astype(np.uint16)

In [14]: df
Out[14]:
   index  fruit  quantity  price
0      0      0         5   0.99
1      1      0         2   0.99
2      2      1         4   0.89
3      4      2         1   1.64
4  10023      3        10   0.92

In [15]: df.dtypes
Out[15]:
index         int64
fruit        uint16
quantity      int64
price       float64
dtype: object
Run Code Online (Sandbox Code Playgroud)

或者你可以这样做:

In [21]: df['fruit'] = df.fruit.astype('category').cat.codes

In [22]: df
Out[22]:
   index  fruit  quantity  price
0      0      0         5   0.99
1      1      0         2   0.99
2      2      3         4   0.89
3      4      1         1   1.64
4  10023      2        10   0.92

In [23]: df.dtypes
Out[23]:
index         int64
fruit          int8
quantity      int64
price       float64
dtype: object
Run Code Online (Sandbox Code Playgroud)