【问题标题】:using Pandas to download/load zipped csv file from URL使用 Pandas 从 URL 下载/加载压缩的 csv 文件
【发布时间】:2016-12-19 08:06:57
【问题描述】:

我正在尝试使用 Python 3.5 和 Pandas 将 csv 文件从以下 URL 加载到数据框中:

link = "http://api.worldbank.org/v2/en/indicator/NY.GDP.MKTP.CD?downloadformat=csv"

csv 文件 (API_NY.GDP.MKTP.CD_DS2_en_csv_v2.csv) 位于 zip 文件中。我的尝试:

import urllib.request
urllib.request.urlretrieve(link, "GDP.zip")
import zipfile
compressed_file = zipfile.ZipFile('GDP.zip')
csv_file = compressed_file.open('API_NY.GDP.MKTP.CD_DS2_en_csv_v2.csv')
GDP = pd.read_csv(csv_file)

但在阅读时,我收到错误“pandas.io.common.CParserError: Error tokenizing data. C error: Expected 3 fields in line 5, saw 62”。

有什么想法吗?

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    我认为您需要参数 skiprows,因为 csv 标头在行中 5:

    GDP = pd.read_csv(csv_file, skiprows=4)
    print (GDP.head())
      Country Name Country Code     Indicator Name  Indicator Code          1960  \
    0        Aruba          ABW  GDP (current US$)  NY.GDP.MKTP.CD           NaN   
    1      Andorra          AND  GDP (current US$)  NY.GDP.MKTP.CD           NaN   
    2  Afghanistan          AFG  GDP (current US$)  NY.GDP.MKTP.CD  5.377778e+08   
    3       Angola          AGO  GDP (current US$)  NY.GDP.MKTP.CD           NaN   
    4      Albania          ALB  GDP (current US$)  NY.GDP.MKTP.CD           NaN   
    
               1961          1962          1963          1964          1965  \
    0           NaN           NaN           NaN           NaN           NaN   
    1           NaN           NaN           NaN           NaN           NaN   
    2  5.488889e+08  5.466667e+08  7.511112e+08  8.000000e+08  1.006667e+09   
    3           NaN           NaN           NaN           NaN           NaN   
    4           NaN           NaN           NaN           NaN           NaN   
    
                  2008          2009          2010          2011  \
    0     ...       2.791961e+09  2.498933e+09  2.467704e+09  2.584464e+09   
    1     ...       4.001201e+09  3.650083e+09  3.346517e+09  3.427023e+09   
    2     ...       1.019053e+10  1.248694e+10  1.593680e+10  1.793024e+10   
    3     ...       8.417803e+10  7.549238e+10  8.247091e+10  1.041159e+11   
    4     ...       1.288135e+10  1.204421e+10  1.192695e+10  1.289087e+10   
    
               2012          2013          2014          2015  2016  Unnamed: 61  
    0           NaN           NaN           NaN           NaN   NaN          NaN  
    1  3.146152e+09  3.248925e+09           NaN           NaN   NaN          NaN  
    2  2.053654e+10  2.004633e+10  2.005019e+10  1.933129e+10   NaN          NaN  
    3  1.153984e+11  1.249121e+11  1.267769e+11  1.026269e+11   NaN          NaN  
    4  1.231978e+10  1.278103e+10  1.321986e+10  1.139839e+10   NaN          NaN  
    

    【讨论】:

    • 为简化起见,我省略了该论点。我之前尝试过,但我收到了这个错误:“pandas.io.common.EmptyDataError: No columns to parse from file”。顺便说一句,我正在使用 Mac OS Sierra 和 PyCharm Community Edition 2016.3。关于为什么加载数据失败的任何想法?
    • 嗯,可能是下载或者解压有问题,测试print csv_file.readlines()[:3]
    • 我收到了这个@jezrael:[b'\xef\xbb\xbf"Data Source","World Development Indicators",\r\n', b'\r\n', b'"Last Updated Date","2016-12-16",\r\n'] 。有什么线索吗?
    • 只有一个想法——你的熊猫版本是什么? print (pd.show_versions())
    • 相同:0.19.1。但我一直在使用 PyCharm。我刚刚在终端上尝试过,它运行良好。谢谢!顺便说一句,知道为什么它不适用于 PyCharm 吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 2018-09-19
    • 2014-05-26
    • 2021-05-12
    • 1970-01-01
    • 2019-01-31
    • 2021-09-04
    相关资源
    最近更新 更多