我已閱讀有關此主題的幾個問題,但似乎沒有爲我工作。閱讀網址爲熊貓數據框與列名(python3)
我想從這個頁面檢索數據「http://archive.ics.uci.edu/ml/machine-learning-databases/statlog/heart/heart.dat」,併爲這些列指定了一些名稱。
我的代碼如下,這並不讓我指定名稱的數據列,因爲一切都在一列:
import pandas as pd
import io
import requests
url="http://archive.ics.uci.edu/ml/machine-learningdatabases/statlog/heart/heart.dat"
s=requests.get(url).content
header_row = ['age','sex','chestpain','restBP','chol','sugar','ecg','maxhr','angina','dep','exercise','fluor','thal','diagnosis']
c=pd.read_csv(io.StringIO(s.decode('utf-8')), names=header_row)
print(c)
輸出是:
age sex chestpain \
0 70.0 1.0 4.0 130.0 322.0 0.0 2.0 109.0 0.0 2.4... NaN NaN
1 67.0 0.0 3.0 115.0 564.0 0.0 2.0 160.0 0.0 1.6... NaN NaN
2 57.0 1.0 2.0 124.0 261.0 0.0 0.0 141.0 0.0 0.3... NaN NaN
3 64.0 1.0 4.0 128.0 263.0 0.0 0.0 105.0 1.0 0.2... NaN NaN
我需要做些什麼來實現我的目標?
非常感謝!
你確定的網址。我在打開它時遇到404錯誤 –
正確的網址https://archive.ics.uci.edu/ml/machine-learning-databases/statlog/heart/heart.dat –