Soumyajit Chatterjee
Soumyajit Chatterjee

Reputation: 63

Getting KeyError in Pandas Dataframe after Filtering

I am trying to access a row in the pandas' data frame after applying a columnwise selection. However, It is giving an error on certain row index (here row index 2, albeit I can access row 3) which is present in the filtered data frame. Can someone please explain this and help me finding an workaround so that I can access the data frame in normal flow of indices (i.e. 0,1,2...). Thanks in advance.

>>> import pandas as pd
>>> a = pd.read_csv("sample.csv")
>>> a
   Col1  Col2  Col3
0     1     2     1
1     5     9     1
2     6     9     0
3     2     7     1
>>> a = a[a['Col3']>0]
>>> a
   Col1  Col2  Col3
0     1     2     1
1     5     9     1
3     2     7     1
>>> a['Col1'][0]
1
>>> a['Col1'][2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/series.py", line 767, in __getitem__
    result = self.index.get_value(self, key)
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py", line 3118, in get_value
    tz=getattr(series.dtype, 'tz', None))
  File "pandas/_libs/index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 114, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 958, in pandas._libs.hashtable.Int64HashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 964, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 2
>>> a['Col1'][1]
5
>>> a['Col1'][3]
2

Upvotes: 1

Views: 481

Answers (2)

StupidWolf
StupidWolf

Reputation: 46888

You use iloc for purely integer based indexing:

a = pd.DataFrame({'Col1':[1,5,6,2],'Col2':[2,9,9,7],'Col3':[1,1,0,1]})
a = a[a['Col3']>0]
a.iloc[2]

Col1    2
Col2    7
Col3    1

Upvotes: 0

Andrej Kesely
Andrej Kesely

Reputation: 195408

You can use .iat[]:

a = a[a['Col3']>0]
print(a)
print(a['Col1'].iat[2])  # prints 2

Prints:

   Col1  Col2  Col3
0     1     2     1
1     5     9     1
3     2     7     1
2

Upvotes: 1

Related Questions