daframe如下:
weather_main列中含有多种类别,现在需要把它们映射为整数:
- 先输出类别:
listType = df['weather_main'].unique()
print(listType)
输出如下:
['clear' 'clouds' 'rain' 'snow' 'drizzle' 'thunderstorm' 'mist' 'fog' 'dust']
- 映射:
wm_dic = {'clear': 0, 'clouds': 1, 'rain': 2, 'snow': 3, 'drizzle': 4, 'thunderstorm': 5, 'mist': 6, 'fog': 7, 'dust': 8}
df['weather_main'] = df['weather_main'].map(dic)
如果种类很多,字典不好写,可以利用字典生成函数:
dic = dict.fromkeys(listType)
for i in range(len(listType)):
dic[listType[i]] = i
df['weather_main'] = df['weather_main'].map(dic)