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

2024-06-02 08:06:36 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我从网上搜集的数据:

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, Never-married, Adm-clerical, Not-in-family, White, Male, 2174, 0, 40, United-States, <=50K 50, Self-emp-not-inc, 83311, Bachelors, 13, Married-civ-spouse, Exec-man

我想向数据中添加以下标题,以便构建分类器

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上运行我的数据


Tags: 数据httpsimportgetmachinerequestsmlclass
1条回答
网友
1楼 · 发布于 2024-06-02 08:06:36

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

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

熊猫可以是一个更重的方法,有很多用户工具:

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非常受欢迎

相关问题 更多 >