2

Which method is better (in terms of performance and reliability) to use in order to fetch single cells from a pandas DataFrame: get_value() or loc[]?

intael
  • 508
  • 2
  • 7
  • 21

1 Answers1

5

You can find info in docs in the end:

For getting a value explicitly (equiv to deprecated df.get_value('a','A'))

# this is also equivalent to ``df1.at['a','A']``
In [55]: df1.loc['a', 'A']
Out[55]: 0.13200317033032932

but if use it there is no warning.

But if check Index.get_value:

Fast lookup of value from 1-dimensional ndarray. Only use this if you know what you’re doing

So I think better is use iat, at, loc, ix.

Timings:

df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df)

In [93]: %timeit (df.loc[0, 'A'])
The slowest run took 6.40 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 177 µs per loop

In [96]: %timeit (df.at[0, 'A'])
The slowest run took 17.01 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 7.61 µs per loop

In [94]: %timeit (df.get_value(0, 'A'))
The slowest run took 23.49 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 3.36 µs per loop
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • I don't understand this sentence in docs about `deprecated get_value()`... Pandas itself uses `get_value()` internally - https://github.com/pandas-dev/pandas/blob/master/pandas/core/indexing.py#L77 – MaxU - stand with Ukraine Nov 10 '16 at 09:45
  • To see the difference between iat/iloc, at/loc, and ix see [this question](http://stackoverflow.com/questions/31593201/pandas-iloc-vs-ix-vs-loc-explanation) – Julien Marrec Nov 10 '16 at 09:46
  • @JulienMarrec - thank you. I found also [this](http://stackoverflow.com/a/28980433/2901002) – jezrael Nov 10 '16 at 11:25