用pandas讀取github的在線數據

用pandas讀取github在線數據。

方式一:

import pandas as pd
url="https://raw.githubusercontent.com/hunkim/DeepLearningZeroToAll/master/data-03-diabetes.csv"
c=pd.read_csv(url,header=None)

方式二:

import pandas as pd
import io
import requests
url="https://raw.githubusercontent.com/hunkim/DeepLearningZeroToAll/master/data-03-diabetes.csv"
s=requests.get(url).content
c=pd.read_csv(io.StringIO(s.decode('utf-8')),header=None)

pandas.read_csv needs a file-like object as the first argument.
所以需要通過io.StringIO函數進行轉換。

注意要選擇csv文件的raw URL。
如直接用:
https://githubusercontent.com/hunkim/DeepLearningZeroToAll/master/data-03-diabetes.csv“,就會報錯。
需轉換爲:
https://raw.githubusercontent.com/hunkim/DeepLearningZeroToAll/master/data-03-diabetes.csv

發佈了34 篇原創文章 · 獲贊 6 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章