简体   繁体   English

我正在使用LabelEncorder将数据集从String更改为Numeric

[英]Using LabelEncorder i am trying to change the dataset from String to Numeric

I have 2 dataset train_df and test_df. 我有2个数据集train_df和test_df。 I need to change the columns ('ReqName','AreaChange', 'CodeChange','TestSuite') as Numeric. 我需要将列(“ ReqName”,“ AreaChange”,“ CodeChange”,“ TestSuite”)更改为数字。 Hence i have used LabelEncorder , while doing so, i get below error 因此,在使用LabelEncorder的同时,出现以下错误

from sklearn.preprocessing import LabelEncoder

data = [train_df, test_df]
cols = ( 'ReqName','AreaChange', 'CodeChange','TestSuite')

for c in cols:
    lbl = LabelEncoder() 
    lbl.fit(list(data[c].values)) 
    data[c] = lbl.transform(list(data[c].values))

I get this error: 我收到此错误:

"TypeError: list indices must be integers or slices, not str" “ TypeError:列表索引必须是整数或切片,而不是str”

You're trying to access list element via str objects ('ReqName','AreaChange' ...) 您正在尝试通过str对象('ReqName','AreaChange'...)访问列表元素

Change for loop to this: 更改for循环:

for c, _ in enumerate(cols):

your error message could not be more clear : 您的错误消息无法更清晰地显示:

you are trying to access the data list in index c which is a string. 您正在尝试访问索引cdata列表,它是一个字符串。

you must access items in list with the corresponding index - which should be integer, for example 1,2,3 etc. 必须使用相应的索引访问列表中的项目-索引应为整数,例如1,2,3等。

so just access it with data[1] , data[0] etc. 因此只需使用data[1]data[0]等访问它即可。

我正在尝试使用显示 URLError 的 seaborn 加载数据集: <urlopen error [winerror 10060]< div><div id="text_translate"><p> df = sns.load_dataset("提示")</p><p> 我正在尝试使用显示 URLError 的 seaborn 加载数据集:</p><p> 几秒钟后抛出此错误</p><pre>TimeoutError Traceback (most recent call last) File ~\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py:1348, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args) 1347 try: -> 1348 h.request(req.get_method(), req.selector, req.data, headers, 1349 encode_chunked=req.has_header('Transfer-encoding')) 1350 except OSError as err: # timeout error File ~\AppData\Local\Programs\Python\Python311\Lib\http\client.py:1282, in HTTPConnection.request(self, method, url, body, headers, encode_chunked) 1281 """Send a complete request to the server.""" -> 1282 self._send_request(method, url, body, headers, encode_chunked) File ~\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py:241, in urlretrieve(url, filename, reporthook, data) 224 """ 225 Retrieve a URL into a temporary location on disk. 226 (...) 237 data file as well as the resulting HTTPMessage object. 238 """ 239 url_type, path = _splittype(url) --> 241 with contextlib.closing(urlopen(url, data)) as fp: 242 headers = fp.info() 244 # Just return the local path and the "headers" for file:// 245 # URLs. No sense in performing a copy unless requested. URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond></pre><p> 我尝试更改 inte.net 连接,还尝试取消选中 LAN 设置中的代理服务器</p></div></urlopen> - I am trying to load dataset using seaborn showing URLError: <urlopen error [WinError 10060]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使用pandas旋转数据集 - I am trying to pivot a dataset with pandas 我正在尝试使用神经网络使用 tensorflow 对波士顿数据集进行回归。 不知何故,我把所有的预测都弄错了 - I am trying to use Neural Networks for regression on boston dataset using tensorflow. Somehow I am getting all predictions as wrong 我正在尝试重新调整与 MinMaxscaler 方法略有不同的数据框中的数字列 - I am trying to rescale numeric columns in a dataframe that is slightly different from the MinMaxscaler approach 我正在尝试使用显示 URLError 的 seaborn 加载数据集: <urlopen error [winerror 10060]< div><div id="text_translate"><p> df = sns.load_dataset("提示")</p><p> 我正在尝试使用显示 URLError 的 seaborn 加载数据集:</p><p> 几秒钟后抛出此错误</p><pre>TimeoutError Traceback (most recent call last) File ~\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py:1348, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args) 1347 try: -> 1348 h.request(req.get_method(), req.selector, req.data, headers, 1349 encode_chunked=req.has_header('Transfer-encoding')) 1350 except OSError as err: # timeout error File ~\AppData\Local\Programs\Python\Python311\Lib\http\client.py:1282, in HTTPConnection.request(self, method, url, body, headers, encode_chunked) 1281 """Send a complete request to the server.""" -> 1282 self._send_request(method, url, body, headers, encode_chunked) File ~\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py:241, in urlretrieve(url, filename, reporthook, data) 224 """ 225 Retrieve a URL into a temporary location on disk. 226 (...) 237 data file as well as the resulting HTTPMessage object. 238 """ 239 url_type, path = _splittype(url) --> 241 with contextlib.closing(urlopen(url, data)) as fp: 242 headers = fp.info() 244 # Just return the local path and the "headers" for file:// 245 # URLs. No sense in performing a copy unless requested. URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond></pre><p> 我尝试更改 inte.net 连接,还尝试取消选中 LAN 设置中的代理服务器</p></div></urlopen> - I am trying to load dataset using seaborn showing URLError: <urlopen error [WinError 10060] 我正在尝试从特定格式的字符串中提取日期和时间 - I am trying to pull the date and time from a specifically formatted string 如何使用 spark.sql 从 MySql 写下一行? 我正在尝试将字符串转换为日期格式 - How to write below line from MySql using spark.sql? I am trying to Convert string to Date format 我正在尝试将字符串格式的日期更改为 python 中的日期格式 - I am trying to change date which is in string format to date format in python 我正在尝试使用python从S3存储桶中删除文件,但无法删除该文件 - I am trying to delete file from S3 bucket using python but I am not able to delete the file 我正在尝试使用 python 从该网站下载年度数据,但我不知道如何处理它? - I am trying to download the Yearly data from this website using python but i am not sure how to approach it? 我正在尝试按日期索引数据集,我做错了什么? - I am trying to index dataset by date, what am i doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM