【问题标题】:How to read CSV file from GitHub using pandas如何使用 pandas 从 GitHub 读取 CSV 文件
【发布时间】:2019-08-09 22:34:51
【问题描述】:

我正在尝试使用 pandas 使用 Python 读取 github 上的 CSV 文件> 我浏览了整个网络,并尝试了一些在此网站上找到的解决方案,但它们不起作用。我做错了什么?

我试过这个:

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))

【问题讨论】:

  • 将 url 设置为“原始”视图 https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv
  • @ChrisA 这很好,谢谢!您能告诉我您是如何获得raw 视图的吗?我看到你的链接没有github.comblob
  • 是的,如果您转到原始链接。在主窗口上方,右侧有 3 个按钮 raw, blame, history。点击原始
  • 谢谢我的朋友

标签: python pandas csv


【解决方案1】:

您应该提供原始内容的 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))

输出:

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

【讨论】:

  • 或者,您可以简单地在 GitHub URL 的末尾添加“?raw=true”。您可以在下面查看我的答案以查看代码的外观。
  • 使用 gitlab 公共存储库执行此操作时,我收到了 HTTP 错误 (HTTP Error 403: Forbidden)。有没有办法对 gitlab 上的原始链接做同样的事情?
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    在 GitHub URL 末尾添加 ?raw=true 以获取原始文件链接。

    在你的情况下,

    import pandas as pd
    url = 'https://github.com/lukes/ISO-3166-Countries-with-Regional-Codes/blob/master/all/all.csv?raw=true'
    df = pd.read_csv(url,index_col=0)
    
    print(df.head(5))
    

    输出:

                   alpha-2 alpha-3  country-code     iso_3166-2   region  \
    name                                                                   
    Afghanistan         AF     AFG             4  ISO 3166-2:AF     Asia   
    Åland Islands       AX     ALA           248  ISO 3166-2:AX   Europe   
    Albania             AL     ALB             8  ISO 3166-2:AL   Europe   
    Algeria             DZ     DZA            12  ISO 3166-2:DZ   Africa   
    American Samoa      AS     ASM            16  ISO 3166-2:AS  Oceania   
    
                         sub-region intermediate-region  region-code  \
    name                                                               
    Afghanistan       Southern Asia                 NaN        142.0   
    Åland Islands   Northern Europe                 NaN        150.0   
    Albania         Southern Europe                 NaN        150.0   
    Algeria         Northern Africa                 NaN          2.0   
    American Samoa        Polynesia                 NaN          9.0   
    
                    sub-region-code  intermediate-region-code  
    name                                                       
    Afghanistan                34.0                       NaN  
    Åland Islands             154.0                       NaN  
    Albania                    39.0                       NaN  
    Algeria                    15.0                       NaN  
    American Samoa             61.0                       NaN 
    

    注意:这仅适用于 GitHub 链接,不适用于 GitLab 或 Bitbucket 链接。

    【讨论】:

    • 请问为什么当我读取文件并打印它时,它只显示第 0-20 行然后跳到 3000 并一直到最后。注意:有 5000 行。
    • @AirStalk3r 您需要提供更多信息。可能会发布一个包含详细信息的新问题。
    【解决方案4】:

    您可以复制/粘贴网址并更改两件事:

    1. 删除“blob”
    2. 将 github.com 替换为 raw.githubusercontent.com

    例如这个链接:

    https://github.com/mwaskom/seaborn-data/blob/master/iris.csv
    

    这样工作:

    import pandas as pd
    
    pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
    

    【讨论】:

      【解决方案5】:

      首先将 github csv 文件转换为 raw 以便访问数据,点击下面的链接评论如何将 csv 文件转换为 raw 。

      import pandas as pd
      
      url_data = (r'https://raw.githubusercontent.com/oderofrancis/rona/main/Countries-Continents.csv')
      
      data_csv = pd.read_csv(url_data)
      
      data_csv.head()
      

      【讨论】:

      猜你喜欢
      • 2020-03-22
      • 2014-11-15
      • 2021-09-24
      • 1970-01-01
      • 2023-02-20
      相关资源
      最近更新 更多