Python - 压缩和列表

时间:2017-05-09 02:42:59

标签: python list zip

几周前我刚刚开始编写python并弄脏手。但是我无法通过压缩和列表解决问题。

这是我的代码:

import pandas as pd

df_reader = pd.read_csv('Indicators.csv', chunksize=1000)

df_urb_pop = next(df_reader)

df_pop_ceb = df_urb_pop[df_urb_pop['CountryCode']=='CEB']

zipped = zip(df_pop_ceb['Population, total'], df_pop_ceb['Urban population (% of total)'])

pops_list = list(zipped)

print(pops_list)

这是我得到的错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/indexes/base.py", line 2134, in get_loc
    return self._engine.get_loc(key)
  File "pandas/index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas/index.c:4433)
  File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:4279)
  File "pandas/src/hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13742)
  File "pandas/src/hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13696)
KeyError: 'Population, total'

在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last):
  File "/Users/mubashirsultan/PycharmProjects/TECH6360/Iterators practice.py", line 9, in <module>
    zipped = zip(df_pop_ceb['Population, total'], df_pop_ceb['Urban population (% of total)'])
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 2059, in __getitem__
    return self._getitem_column(key)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 2066, in _getitem_column
    return self._get_item_cache(key)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/generic.py", line 1386, in _get_item_cache
    values = self._data.get(item)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/internals.py", line 3543, in get
    loc = self.items.get_loc(item)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/indexes/base.py", line 2136, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas/index.c:4433)
  File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:4279)
  File "pandas/src/hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13742)
  File "pandas/src/hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13696)
KeyError: 'Population, total'

Process finished with exit code 1

不太确定我犯了什么错误。一点帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

我已从您提供的链接下载Indicators.csv,csv文件的标头只包含以下密钥:

import pandas as pd
df_reader = pd.read_csv('Indicators.csv')
df_reader.keys()

输出:

Index([u'CountryName', u'CountryCode', u'IndicatorName', u'IndicatorCode',
       u'Year', u'Value'],
      dtype='object')

没有钥匙&#39; Population, total&#39;和&#39; Urban population (% of total)&#39;,可能您使用了错误的数据源(csv文件)。