2017-01-29 4 views

Antwort

1

df.loc[index, 'col name'] ist mehr idiomatische und bevorzugt, vor allem, wenn Sie Zeilen

Demo filtern möchten: für 1.000.000 x 3 Form DF

In [26]: df = pd.DataFrame(np.random.rand(10**6,3), columns=list('abc')) 

In [27]: %timeit df[df.a < 0.5]['a'] 
10 loops, best of 3: 45.8 ms per loop 

In [28]: %timeit df.loc[df.a < 0.5]['a'] 
10 loops, best of 3: 45.8 ms per loop 

In [29]: %timeit df.loc[df.a < 0.5, 'a'] 
10 loops, best of 3: 37 ms per loop 

Für den Bau, wo Sie nur eine Spalte und don brauchen‘ t Filterreihen wie df[:]['Store'] - es ist besser, einfach df['Store'] zu verwenden:

In [30]: %timeit df[:]['a'] 
1000 loops, best of 3: 436 µs per loop 

In [31]: %timeit df.loc[:]['a'] 
10000 loops, best of 3: 25.9 µs per loop 

In [36]: %timeit df['a'].loc[:] 
10000 loops, best of 3: 26.5 µs per loop 

In [32]: %timeit df.loc[:, 'a'] 
10000 loops, best of 3: 126 µs per loop 

In [33]: %timeit df['a'] 
The slowest run took 5.08 times longer than the fastest. This could mean that an intermediate result is being cached. 
100000 loops, best of 3: 8.17 µs per loop 

Uncoditional Zugang von multip Säulen:

In [34]: %timeit df[['a','b']] 
10 loops, best of 3: 22 ms per loop 

In [35]: %timeit df.loc[:, ['a','b']] 
10 loops, best of 3: 22.6 ms per loop 
Verwandte Themen