簡體   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