0

The following code works perfectly with row 0, exact march with 'A', but throws an error for other rows, here exact match with 'B'. Any idea why?

df = pd.DataFrame({'Column1': ['A', 'B', 'C'], 'Column2': [0, 1, 2]})
print(df.loc[df["Column1"] == 'A'].Column2[0])
df = pd.DataFrame({'Column1': ['A', 'B', 'C'], 'Column2': [0, 1, 2]})
print(df.loc[df["Column1"] == 'B'].Column2[0])

Here is the error message:

Traceback (most recent call last):

  File "<ipython-input-105-749b69acaacd>", line 2, in <module>
    print(df.loc[df["Column1"] == 'B'].Column2[0])

  File "/anaconda3/lib/python3.7/site-packages/pandas/core/series.py", line 868, in __getitem__
    result = self.index.get_value(self, key)

  File "/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 4375, in get_value
    tz=getattr(series.dtype, 'tz', None))

  File "pandas/_libs/index.pyx", line 81, in pandas._libs.index.IndexEngine.get_value

  File "pandas/_libs/index.pyx", line 89, in pandas._libs.index.IndexEngine.get_value

  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/hashtable_class_helper.pxi", line 987, in pandas._libs.hashtable.Int64HashTable.get_item

  File "pandas/_libs/hashtable_class_helper.pxi", line 993, in pandas._libs.hashtable.Int64HashTable.get_item
paragui
  • 15
  • 6
  • 2
    When you slice with `[0]` you are looking for the index labeled `0`. This exists when you slice by `A` because this includes the first row (indexed at 0) but the slice when `==B` starts at index 1. If you want to always grab the first row you should use `.iloc[0]` – ALollz Sep 22 '19 at 20:41
  • If your goal is to get the first row, given a conditon, this may be a suitable dupe target: https://stackoverflow.com/questions/40660088/get-first-row-of-dataframe-in-python-pandas-based-on-criteria – ALollz Sep 22 '19 at 20:45

1 Answers1

0

@ALollz has already explained the reason. I just wanted to point out that when you are debugging it will be good to check individual parts of the expression in REPL. Then you can get to the conclusion easily.

>>> df["Column1"] == "B"
0    False
1     True
2    False
Name: Column1, dtype: bool
>>> df.loc[df["Column1"] == "B"]
  Column1  Column2
1       B        1
>>> df.loc[df["Column1"] == "A"]
  Column1  Column2
0       A        0
abhilb
  • 5,639
  • 2
  • 20
  • 26