Преобразовать уникальные строки в уникальные числа (factorization) в Pandas.DataFrame

338 просмотра
0
0 Комментариев

Есть набор данных в котором содержится поле ‘Country’.Количество уникальных значений более 100. Как присвоить каждой стране соответствующий уникальный номер?

      Measure           Country             Citizenship  Year    Value
0            0           Oceania     New Zealand Citizen  1979  11817.0
1            0           Oceania      Australian Citizen  1979   4436.0
2            0           Oceania  Total All Citizenships  1979  19965.0
3            0        Antarctica     New Zealand Citizen  1979     10.0
4            0        Antarctica      Australian Citizen  1979      0.0
5            0        Antarctica  Total All Citizenships  1979     13.0
6            0    American Samoa     New Zealand Citizen  1979     17.0
7            0    American Samoa      Australian Citizen  1979      4.0
8            0    American Samoa  Total All Citizenships  1979     30.0
9            0         Australia     New Zealand Citizen  1979   8224.0
10           0         Australia      Australian Citizen  1979   4092.0
11           0         Australia  Total All Citizenships  1979  13037.0
12           0     Cocos Islands     New Zealand Citizen  1979      0.0
13           0     Cocos Islands      Australian Citizen  1979      0.0
14           0     Cocos Islands  Total All Citizenships  1979      0.0
15           0      Cook Islands     New Zealand Citizen  1979    317.0
16           0      Cook Islands      Australian Citizen  1979      5.0
17           0      Cook Islands  Total All Citizenships  1979    807.0
18           0  Christmas Island     New Zealand Citizen  1979      0.0
19           0  Christmas Island      Australian Citizen  1979      0.0
20           0  Christmas Island  Total All Citizenships  1979      0.0
21           0              Fiji     New Zealand Citizen  1979    358.0
22           0              Fiji      Australian Citizen  1979      9.0
23           0              Fiji  Total All Citizenships  1979    680.0
24           0        Micronesia     New Zealand Citizen  1979      2.0
25           0        Micronesia      Australian Citizen  1979      0.0
26           0        Micronesia  Total All Citizenships  1979      4.0
27           0              Guam     New Zealand Citizen  1979      0.0
28           0              Guam      Australian Citizen  1979      0.0
29           0              Guam  Total All Citizenships  1979      1.0
...        ...               ...                     ...   ...      ...
86496        2          Tanzania     New Zealand Citizen  2016      4.0
86497        2          Tanzania      Australian Citizen  2016      0.0
86498        2          Tanzania  Total All Citizenships  2016     16.0
86499        2            Uganda     New Zealand Citizen  2016      8.0
86500        2            Uganda      Australian Citizen  2016      0.0
86501        2            Uganda  Total All Citizenships  2016     14.0
86502        2       South Yemen     New Zealand Citizen  2016      0.0
86503        2       South Yemen      Australian Citizen  2016      0.0
86504        2       South Yemen  Total All Citizenships  2016      0.0
86505        2             Yemen     New Zealand Citizen  2016      0.0
86506        2             Yemen      Australian Citizen  2016      0.0
86507        2             Yemen  Total All Citizenships  2016      6.0
86508        2           Mayotte     New Zealand Citizen  2016      0.0
86509        2           Mayotte      Australian Citizen  2016      0.0
86510        2           Mayotte  Total All Citizenships  2016      0.0
86511        2      South Africa     New Zealand Citizen  2016     53.0
86512        2      South Africa      Australian Citizen  2016      4.0
86513        2      South Africa  Total All Citizenships  2016   4297.0
86514        2            Zambia     New Zealand Citizen  2016     -4.0
86515        2            Zambia      Australian Citizen  2016      0.0
86516        2            Zambia  Total All Citizenships  2016     19.0
86517        2          Zimbabwe     New Zealand Citizen  2016      4.0
86518        2          Zimbabwe      Australian Citizen  2016      2.0
86519        2          Zimbabwe  Total All Citizenships  2016     63.0
86520        2        Not stated     New Zealand Citizen  2016    908.0
86521        2        Not stated      Australian Citizen  2016     80.0
86522        2        Not stated  Total All Citizenships  2016   3177.0
86523        2     All countries     New Zealand Citizen  2016  -1818.0
86524        2     All countries      Australian Citizen  2016   3487.0
86525        2     All countries  Total All Citizenships  2016  70588.0
 
[86526 rows x 5 columns]

введите сюда описание изображения


Добавить комментарий

1 Ответы

Python Опубликовано 12.12.2018
0

Воспользуйтесь pd.factorize():

In [17]: df['CountryID'] = pd.factorize(df.Country)[0]
 
In [18]: df
Out[18]:
    Measure         Country             Citizenship  Year    Value  CountryID
0         0         Oceania     New Zealand Citizen  1979  11817.0          0
1         0         Oceania      Australian Citizen  1979   4436.0          0
2         0         Oceania  Total All Citizenships  1979  19965.0          0
3         0      Antarctica     New Zealand Citizen  1979     10.0          1
4         0      Antarctica      Australian Citizen  1979      0.0          1
5         0      Antarctica  Total All Citizenships  1979     13.0          1
6         0  American Samoa     New Zealand Citizen  1979     17.0          2
7         0  American Samoa      Australian Citizen  1979      4.0          2
8         0  American Samoa  Total All Citizenships  1979     30.0          2
9         0       Australia     New Zealand Citizen  1979   8224.0          3
..      ...             ...                     ...   ...      ...        ...
50        2          Zambia  Total All Citizenships  2016     19.0         16
51        2        Zimbabwe     New Zealand Citizen  2016      4.0         17
52        2        Zimbabwe      Australian Citizen  2016      2.0         17
53        2        Zimbabwe  Total All Citizenships  2016     63.0         17
54        2      Not stated     New Zealand Citizen  2016    908.0         18
55        2      Not stated      Australian Citizen  2016     80.0         18
56        2      Not stated  Total All Citizenships  2016   3177.0         18
57        2   All countries     New Zealand Citizen  2016  -1818.0         19
58        2   All countries      Australian Citizen  2016   3487.0         19
59        2   All countries  Total All Citizenships  2016  70588.0         19
 
[60 rows x 6 columns]

Добавить комментарий
Напишите свой ответ на данный вопрос.
Scroll Up