Pandas DataFrame search is linear time or constant time?

Viewed 13531

I have a dataframe object df of over 15000 rows like:

anime_id          name              genre    rating
1234      Kimi no nawa    Romance, Comedy     9.31
5678       Stiens;Gate             Sci-fi     8.92

And I am trying to find the row with a particular anime_id.

a_id = "5678"
temp = (df.query("anime_id == "+a_id).genre)

I just wanted to know if this search was done in constant time (like dictionaries) or linear time(like lists).

3 Answers

You should note that even iloc is about 2 orders of magnitude slower then hashmap when your index is unique:

df = pd.DataFrame(np.random.randint(0, 10**7, 10**5), columns=['a'])
%timeit df.iloc[random.randint(0,10**5)]
10000 loops, best of 3: 51.5 µs per loop

s = set(np.random.randint(0, 10**7, 10**5))
%timeit random.randint(0,10**7) in s
The slowest run took 9.70 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 615 ns per loop
Related