如何生成字典分类值
问题描述:
我有一个熊猫数据帧恶魔像下面如何生成字典分类值
df.wlan_mgt_fixed_reason_code.unique()
= array(['?', '0x0002', '0x0003', ..., '0x0c3c', '0xbf17', '0x4cee'], dtype=object)
我需要的唯一编号来代替这些独特的价值观,我想通过一个神经网络来运行数据。
我需要一本字典唯一值的,这样我可以在下面的方式取而代之。
di =
{
"0x0002" : 2,
"0x0003" : 3,
"0x0001" : 4,
"0x0006" : 5,
"0x0007" : 6,
"0x0008" : 7,
"0x944f" : 8,
"0xda64" : 9,
"0x7415" : 10,
"0x64d7" : 11,
"0x130d" : 12,
"0x39a1" : 13,
"0x5df0" : 14,
"0xc87e" : 15,
"0x744f" : 16,
"0x7983" : 17,
"0x0632" : 18,
"0x3922" : 19,
"0x2c60" : 20,
"0xa5d9" : 21,
"0x02b8" : 22,
"0x71c4" : 23,
"0x0c3c" : 24,
"0xbf17" : 25,
"0x4cee" : 1,
}
然后用字典值替换列。
有一个简单的方法来自动完成这个,或者一个代码段自动识别独特的分类值与序列号替换它们。
答
尝试categorical
:
df.wlan_mgt_fixed_reason_code = df.wlan_mgt_fixed_reason_code.astype('category')
在于所述的数据类型还是蛮新的,这意味着你可能要坚持使用一个标准的矢量器:
uniqs = list(df.wlan_mgt_fixed_reason_code.unique())
uniq_dict = {uniqs[x]: x for x in range(len(uniqs))}
df.wlan_mgt_fixed_reason_code = df.wlan_mgt_fixed_reason_code.replace(uniq_dict)
非常感谢。它非常完美。 –