简单调用数组元素的Pandas Keyerror

时间:2014-08-01 18:07:32

标签: pandas

我正在从Pandas加载.csv。它有一个国家,月份,年份和日期列。由于我只对这些感兴趣,因此我用更简单的版本覆盖导入的数据帧。

df = pd.read_csv('data.csv')
idx_USA = df['Country'] == 'United States'
df = df.loc[:,['Year','Month','Date']]
print(df[:4])

这个日子

   Year Month Date
1  2007    1   1
2  2004     10   2
4  1999     10   14
7  2000     10   5

现在,奇怪的是,当我试图在一个循环中访问这些年份时,我得到了一个keyerror!这很简单 - 发生了什么?感谢。

for i in range(1,N):
    print "Yr = ", df['Year'][i]


Yr =  2007
Yr =  2004
Yr = 
Traceback (most recent call last):
  File "testimport.py", line 19, in <module>
    print "Yr = ", df['Year'][i]
  File "/usr/lib/python2.7/dist-packages/pandas/core/series.py", line 491, in __getitem__
    result = self.index.get_value(self, key)
  File "/usr/lib/python2.7/dist-packages/pandas/core/index.py", line 1032, in get_value
    return self._engine.get_value(s, k)
  File "index.pyx", line 97, in pandas.index.IndexEngine.get_value (pandas/index.c:2957)
  File "index.pyx", line 105, in pandas.index.IndexEngine.get_value (pandas/index.c:2772)
  File "index.pyx", line 149, in pandas.index.IndexEngine.get_loc (pandas/index.c:3498)
  File "hashtable.pyx", line 382, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6930)
  File "hashtable.pyx", line 388, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6871)
KeyError: 3

1 个答案:

答案 0 :(得分:1)

您可以只迭代底层数组,而不是重置索引。

arr = df['year']
for i in range(1,N):
    print arr[i]