如何将分类数据转换为数值数据?

时间:2018-07-12 17:56:42

标签: python pandas

我具有功能=> city,它是分类数据,即字符串,但是不是使用replace()进行硬编码吗?有什么聪明的方法吗?

train['city'].unique()
Output: ['city_149', 'city_83', 'city_16', 'city_64', 'city_100', 'city_21',
       'city_114', 'city_103', 'city_97', 'city_160', 'city_65',
       'city_90', 'city_75', 'city_136', 'city_159', 'city_67', 'city_28',
       'city_10', 'city_73', 'city_76', 'city_104', 'city_27', 'city_30',
       'city_61', 'city_99', 'city_41', 'city_142', 'city_9', 'city_116',
       'city_128', 'city_74', 'city_69', 'city_1', 'city_176', 'city_40',
       'city_123', 'city_152', 'city_165', 'city_89', 'city_36', .......]

我正在尝试什么:

train.replace(['city_149', 'city_83', 'city_16', 'city_64', 'city_100', 'city_21',
           'city_114', 'city_103', 'city_97', 'city_160', 'city_65',
           'city_90', 'city_75', 'city_136', 'city_159', 'city_67', 'city_28',
           'city_10', 'city_73', 'city_76', 'city_104', 'city_27', 'city_30',
           'city_61', 'city_99', 'city_41', 'city_142', 'city_9', 'city_116',
           'city_128', 'city_74', 'city_69', 'city_1', 'city_176', 'city_40',
           'city_123', 'city_152', 'city_165', 'city_89', 'city_36', .......], [1,2,3,4,5,6,7,8,9....], inplace=True)

有没有更好的方法可以将数据转换为数字?因为唯一值的数量为123。 所以我需要对1,2,3,4,... 123中的数字进行硬编码以进行转换。提出一些更好的方法将其转换为数值。

3 个答案:

答案 0 :(得分:4)

尝试pd.factorize()

train['city'] = pd.factorize(train.city)[0]

categorical dtypes

train['city'] = train['city'].astype('category').cat.codes

例如:

>>> train
       city
0  city_151
1  city_149
2  city_151
3  city_149
4  city_149
5  city_149
6  city_151
7  city_151
8  city_150
9  city_151

factorize

train['city'] = pd.factorize(train.city)[0]

>>> train
   city
0     0
1     1
2     0
3     1
4     1
5     1
6     0
7     0
8     2
9     0

astype('category')

train['city'] = train['city'].astype('category').cat.codes

>>> train
   city
0     2
1     0
2     2
3     0
4     0
5     0
6     2
7     2
8     1
9     2

答案 1 :(得分:1)

您可以通过mapping完成此操作:

   value_mapper = dict(zip(train['city'].unique(), np.arange(1, 124)))
    train['city'].map(value_mapper)

或更惯用的categorical data

pd.Categorical(train['city']).codes

答案 2 :(得分:1)

如果您的值始终在整数前带有下划线,则列表理解可能对您有用:

data = [int(x.split('_')[-1]) for x in train['city']]

理解会遍历x中的每个train['city'],将x拆分为下划线分隔的部分,并将最后一部分转换为整数。如果您有多个下划线,例如foo_bar_5,则此方法有效。