Escrevendo código eficiente com pandas
Leonidas Souliotis
PhD Candidate
| S1 | R1 | S2 | R2 | S3 | R3 | S4 | R4 | S5 | R5 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | ♦ | 10 | ♣ | Valete | ♣ | Rei | ♠ | 4 | ♥ | Ás |
| 2 | ♦ | Valete | ♦ | Rei | ♦ | 10 | ♦ | Dama | ♦ | Ás |
| 3 | ♣ | Dama | ♣ | Valete | ♣ | Rei | ♣ | 10 | ♣ | Ás |
| S1 | R1 | S2 | R2 | S3 | R3 | S4 | R4 | S5 | R5 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 10 | 3 | 11 | 3 | 13 | 4 | 4 | 1 | 1 |
| 2 | 2 | 11 | 2 | 13 | 2 | 10 | 2 | 12 | 2 | 1 |
| 3 | 3 | 12 | 3 | 11 | 3 | 13 | 3 | 10 | 3 | 1 |
Sn: naipe da n-ésima carta
1 (Copas), 2 (Ouros), 3 (Paus), 4 (Espadas)
Rn: valor da n-ésima carta
1 (Ás), 2-10, 11 (Valete), 12 (Dama), 13 (Rei)
.loc[] — localiza por nome do índice
# Especificar o intervalo de linhas a selecionar
rows = range(0, 500)
# Cronometrar a seleção de linhas com .loc[]
loc_start_time = time.time()
data.loc[rows]
loc_end_time = time.time()
print("Time using .loc[] : {} sec".format(
loc_end_time - loc_start_time))
Time using .loc[]: 0.001951932 seconds
.iloc[] — localiza por número do índice
# Especificar o intervalo de linhas a selecionar
rows = range(0, 500)
# Cronometrar a seleção de linhas com .iloc[]
iloc_start_time = time.time()
data.iloc[rows]
iloc_end_time = time.time()
print("Time using .iloc[]: {} sec".format(
iloc_end_time - iloc_start_time)
Time using .iloc[] : 0.0007140636 sec
Difference in speed: 173.355592654%
.iloc[] — localiza por número do índice
iloc_start_time = time.time()
data.iloc[:,:3]
iloc_end_time = time.time()
print("Time using .iloc[]: {} sec".format(
iloc_end_time - iloc_start_time))
Time using .iloc[]: 0.00125193595886 sec
Localizar colunas por nome
names_start_time = time.time()
data[['S1', 'R1', 'S2']]
names_end_time = time.time()
print("Time using selection by name: {} sec".format(
names_end_time - names_start_time))
Time using selection by name: 0.000964879989624 sec
Difference in speed: 29.7504324188%
Escrevendo código eficiente com pandas