[英]How to recode numerical to categorical data
I am new to Python, I am studying it for data science purposes. 我是Python的新手,我正在研究它用于数据科学目的。 Right now, I am trying to recode some numerical data (1,2,3 etc) into categories.
现在,我正在尝试将一些数字数据(1,2,3等)重新编码为类别。 It requires a little loop in the end, but I can't get that right.
它最终需要一个小循环,但我无法做到这一点。 It causes a key error 3.
它会导致关键错误3。
The dataset has 21 columns. 数据集有21列。
Can anyone help? 有人可以帮忙吗? Thanks!!
谢谢!!
for col_dic in code_list:
col = col_dic[0]
dic = col_dic[1]
values[col] = [dic[x] for x in values[col]]
It is rather hard to understand what exactly you want to see in result, but the cause of this error is clear: 很难理解你想在结果中看到什么,但是这个错误的原因很明显:
You are iterating through a list of lists. 您正在遍历列表列表。 Every
col_dic
contains a col = col_dic[0]
(string like 'property_type'
) and dic = col_dic[1]
(dictionary). 每个
col_dic
包含col = col_dic[0]
(类似'property_type'
字符串)和dic = col_dic[1]
(字典)。 In the last line you are writing info to a values
dict (I suppose you created it before). 在最后一行中,您将信息写入
values
dict(我想您之前创建过它)。 This error is appearing because dic
doesn't contain a particular key from the values[col]
. 出现此错误是因为
dic
不包含values[col]
的特定键。 For example: 例如:
values[col]
is equal to {1: [], 2: [], 3: []}
and dic
is equal to {1: 'One', 2: 'Two'}
. values[col]
等于{1: [], 2: [], 3: []}
, dic
等于{1: 'One', 2: 'Two'}
。 When you are iterating through values[col]
, you are trying to find a key 3
in dic
. 当您迭代
values[col]
,您正试图在dic
找到键3
。 But it contains no 3
key, so the error is appearing. 但它不包含
3
键,因此出现错误。 You should check that dic
contains this key like this: 你应该检查
dic
包含这个键是这样的:
values_list = []
for v in values[col]:
if v in dic:
values_list.append(dic[v])
values[col] = values_list
Also note that your keys are strings represents integers. 另请注意,您的键是字符串表示整数。 Your error can appear when you try to find a key
'3'
(string) in a dict contains keys like 3
(integers). 当您尝试在dict中找到键
'3'
(字符串)时,可能会出现错误,其中包含3
(整数)等键。 So I suggest you to convert your keys to strings: str(key)
. 所以我建议你把你的密钥转换成字符串:
str(key)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.