Pandas.at vs .loc

I studied how to optimize my code, and ran a method pandas .at. In the documentation

Tag-based quick scan accessory

Similar to loc when providing scalar shortcut based queries. You can also set these indexes.

So, I ran a few samples:

Customization

import pandas as pd
import numpy as np
from string import letters, lowercase, uppercase

lt = list(letters)
lc = list(lowercase)
uc = list(uppercase)

def gdf(rows, cols, seed=None):
    """rows and cols are what you'd pass
    to pd.MultiIndex.from_product()"""
    gmi = pd.MultiIndex.from_product
    df = pd.DataFrame(index=gmi(rows), columns=gmi(cols))
    np.random.seed(seed)
    df.iloc[:, :] = np.random.rand(*df.shape)
    return df

seed = [3, 1415]
df = gdf([lc, uc], [lc, uc], seed)

print df.head().T.head().T

df as follows:

            a                                        
            A         B         C         D         E
a A  0.444939  0.407554  0.460148  0.465239  0.462691
  B  0.032746  0.485650  0.503892  0.351520  0.061569
  C  0.777350  0.047677  0.250667  0.602878  0.570528
  D  0.927783  0.653868  0.381103  0.959544  0.033253
  E  0.191985  0.304597  0.195106  0.370921  0.631576

Lets use .atand .locand guarantee that I get the same

print "using .loc", df.loc[('a', 'A'), ('c', 'C')]
print "using .at ", df.at[('a', 'A'), ('c', 'C')]

using .loc 0.37374090276
using .at  0.37374090276

Check speed with .loc

%%timeit
df.loc[('a', 'A'), ('c', 'C')]

10000 loops, best of 3: 180 µs per loop

Testing Speed ​​Using .at

%%timeit
df.at[('a', 'A'), ('c', 'C')]

The slowest run took 6.11 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 8 µs per loop

It looks like a huge increase in speed. Even at the caching stage, it 6.11 * 8is much faster than180

Question

.at? . , .loc, . :

# small df
sdf = gdf([lc[:2]], [uc[:2]], seed)

print sdf.loc[:, :]

          A         B
a  0.444939  0.407554
b  0.460148  0.465239

as print sdf.at[:, :] TypeError: unhashable type

, , , .

, , .at?

+36
2

: df.get_value 0.21.0. df.at df.iat.


df.at .

df.loc / .

, df.get_value, :

In [25]: %timeit df.loc[('a', 'A'), ('c', 'C')]
10000 loops, best of 3: 187 µs per loop

In [26]: %timeit df.at[('a', 'A'), ('c', 'C')]
100000 loops, best of 3: 8.33 µs per loop

In [35]: %timeit df.get_value(('a', 'A'), ('c', 'C'))
100000 loops, best of 3: 3.62 µs per loop

df.at[...] df.get_value, .

+36

.at, , ( 0.22). :

df = pd.DataFrame([[0, 2, 3], [0, 4, 1], [10, 20, 30]], index=[4, 5, 6], columns=['A', 'B', 'C'])
df2 = df.copy()

    A   B   C
4   0   2   3
5   0   4   1
6  10  20  30

df.at[4, 'B'] = 100

    A    B   C
4   0  100   3
5   0    4   1
6  10   20  30

,

 df.at[4, 'C'] = 10.05

, .at (: int):

    A    B   C
4   0  100  10
5   0    4   1
6  10   20  30

.loc:

df2.loc[4, 'C'] = 10.05

    A   B      C
4   0   2  10.05
5   0   4   1.00
6  10  20  30.00

, ( float int). , :

df.at[5, 'A'] = 'a_string'

ValueError: int() 10: 'a_string'

, , int() , @n1k31t4 ,

df.at[5, 'A'] = '123'

     A   B   C
4    0   2   3
5  123   4   1
6   10  20  30
+18

All Articles