使用Pandas从URL

时间:2016-12-19 08:06:58

标签: python-3.x pandas

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

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:错误标记数据。 C错误:第5行预计3个字段,见62"。

有什么想法吗?

1 个答案:

答案 0 :(得分: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