用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”

你可能感兴趣的:(Python)