Panda .between() + iloc creating odd output - would like to understand why

Hi all,

I am now learning python, know a bit of VBA and C# so have some basic understanding of programming concepts.

I read_csv (from pandas) a csv file, then used iloc to split the columns, so that I could then concatenate the data - no idea if this is the best way to do it, but it is the way I worked out from reading.

I then thought, it would be nice to limit the data output to a range, and read some more.

The code is below, and it works fine - I have put the data output below so you can see whether that is an issue. These are statistics from the UK gov website.

The issue I am having is that changing the variable, so that it isn’t everything, pushed out an exception error.

So if I change the value of the function to: .between(10, 30)

I get this error:

line 3080, in get_loc
return self._engine.get_loc(casted_key)
File “pandas_libs\index.pyx”, line 70, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libs\index.pyx”, line 101, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libs\hashtable_class_helper.pxi”, line 1625, in pandas._libs.hashtable.Int64HashTable.get_item
File “pandas_libs\hashtable_class_helper.pxi”, line 1632, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 0

Which is the cause of this exception:

line 19, in
print(firstRow[i], " - ", secondRow[i])

Removing this offending code block outputs the range limited data, just not in the format that I wanted it in.

I wanted to try and understand why this error was occurring, why limiting the data set would throw the iloc part off and cause an exception?

Thanks!

Code and output:

import pandas as pd
import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()

file_path = filedialog.askopenfilename()

df = pd.read_csv(file_path)
output = df[df["Number of homicides"].between(0, 40)]
count = len(output.index)

firstRow = output.iloc[:, 0]
secondRow = output.iloc[:, 1]

i = 0
while i < count:
    print(firstRow[i], " - ", secondRow[i])
    i += 1
print(count)

Output Data:

Southwark - 40
Brent - 38
Greenwich - 38
Newham - 38
Croydon - 36
Haringey - 30
Redbridge - 29
Tower Hamlets - 29
Ealing - 27
Wandsworth - 27
Lambeth - 26
Enfield - 25
Waltham Forest - 25
Islington - 24
Westminster - 24
Barnet - 22
Camden - 22
Hackney - 22
Lewisham - 21
Barking and Dagenham - 17
Hammersmith and Fulham - 16
Hillingdon - 15
Hounslow - 15
Kensington and Chelsea - 12
Havering - 11
Kingston upon Thames - 9
Merton - 9
Bexley - 8
Harrow - 8
Bromley - 7
Richmond upon Thames - 7
Sutton - 3
32

If it helps, further testing seems to show that you cannot have lower than the maximum number in the range.

So .between(1, 100) works, (39, 100) works, but .between(1, 39) fails.

Not really sure why this happens - why wouldn’t it work when you want to limit the range to something in the middle?