繁体   English   中英

如何将标头添加到我从机器学习数据库中提取的以下数据中

[英]How can I add header to the following data that I am pulling from the machine learning database

这是我从网上获取的数据:

import requests
r=requests.get('https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data')
print(r.text[0:200])

这是打印的内容:

39,State-gov,77516,Bachelors,13,未婚,Adm-clerical,不在家,白人,男性,2174,0,40,美国,<= 50K 50,自我约束-inc,83311,单身汉,13岁已婚公民配偶,执行官

我想将以下标头添加到数据中,以便构建分类器。

col_names = ['age', 'work_class', 'fnlwgt', 'education', 'marital_status', 'occupation', 'relationship', 'race', 'sex', 'capital_gain', 'capital_loss', 'hours_per_week', 'native_country', 'class']

...但是我很难将名称输入到数据中。

我正在colab.research.google.com上运行数据

您可以使用内置的python数据结构。 例如,模式[{header1:value1,header2:value2,...},...]中的字典数组,其中每个字典代表一行。

标准库中的csv阅读器可以提供帮助,例如DictReader: https ://docs.python.org/3.7/library/csv.html#csv.DictReader

使用许多用户工具,Pandas可能是更重的方法:

import pandas as pd
df = pd.read_csv(url, header=None, names=col_names)
# Colab will auto pretty print a df if it is the last line of the cell like so
df.head()

通常,这是我期望在研究/数据科学中看到的方法,其中numpy / pandas非常流行。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM