如何使用 Pandas 从 GitHub 读取 CSV 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55240330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:21:33  来源:igfitidea点击:

How to read CSV file from GitHub using pandas

pythonpandascsv

提问by taga

Im trying to read CSV file thats on github with Python using pandas> i have looked all over the web, and I tried some solution that I found on this website, but they do not work. What am I doing wrong?

我正在尝试使用 Pandas 使用 Python 读取 github 上的 CSV 文件 我究竟做错了什么?

I have tried this:

我试过这个:

import pandas as pd

url = 'https://github.com/lukes/ISO-3166-Countries-with-Regional-Codes/blob/master/all/all.csv'
df = pd.read_csv(url,index_col=0)
#df = pd.read_csv(url)

print(df.head(5))

回答by Alderven

You should provide URL to raw content. Try using this:

您应该提供原始内容的 URL。尝试使用这个:

import pandas as pd

url = 'https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv'
df = pd.read_csv(url, index_col=0)
print(df.head(5))

Output:

输出:

               alpha-2           ...            intermediate-region-code
name                             ...                                    
Afghanistan         AF           ...                                 NaN
?land Islands       AX           ...                                 NaN
Albania             AL           ...                                 NaN
Algeria             DZ           ...                                 NaN
American Samoa      AS           ...                                 NaN

回答by PixelRayn

I recommend to either use pandas as you tried to and others here have explained, or depending on the application, the python csv-handler CommaSeperatedPython, which is a minimalistic wrapper for the native csv-library.

我建议要么像您尝试的那样使用 Pandas,这里的其他人已经解释过,或者根据应用程序使用 python csv-handler CommaSeperatedPython,它是本机 csv-library 的简约包装器。

The library returns the contents of a file as a 2-Dimensional String-Array. It's is in its very early stage though, so if you want to do large scale data-analysis, I would suggest Pandas.

该库以二维字符串数组的形式返回文件的内容。不过它还处于早期阶段,所以如果你想做大规模的数据分析,我会建议 Pandas。