Vectoriseren met NumPy-arrays met .values()

Efficiënte code schrijven met pandas

Leonidas Souliotis

PhD candidate

NumPy in pandas

df = pd.DataFrame({'Col1':[0, 1, 
2, 3, 4, 5, 6]}, dtype=np.int8)
print(df)
|        | Col1 | 
|--------|------| 
| 0      | 0    | 
| 1      | 1    | 
| 2      | 2    | 
| 3      | 3    | 
| 4      | 4    | 
| 5      | 5    | 
| 6      | 6    |
nd = np.array(range(7))
print(nd)

[0 1 2 3 4 5 6]
Efficiënte code schrijven met pandas

NumPy-vectorisatie uitvoeren

start_time = time.time()
poker[['R1', 'R2', 'R3', 'R4', 'R5']].values.sum(axis=1)
print("Time using NumPy vectorization: {} sec(time.time() - start_time))
Resultaten van bovenstaande bewerking berekend in 0.00157618522644 seconden
start_time = time.time()
poker[['R1', 'R2', 'R3', 'R4', 'R5']].sum(axis=1)
print("Results from the above operation calculated in %s seconds" % (time.time() - start_time))
Resultaten van bovenstaande bewerking berekend in 0.00268197059631 seconden
Tijdsverschil: 39,0482%
Efficiënte code schrijven met pandas

Laten we oefenen!

Efficiënte code schrijven met pandas

Preparing Video For Download...