Created
July 22, 2020 12:55
-
-
Save kipronokoech/5dd3e802fd79598b6baf7dadf7333f7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
# generate 15000 intergeter data points randomly between -1000 and 1000 | |
col1= np.random.randint(-1000,1000,15000) | |
#dictionary of data | |
data = { | |
"col1" : col1, | |
} | |
# Pandas DataFrame | |
df = pd.DataFrame(data) | |
# We will test the execution time with 5 loops by multiplying each item of | |
# col1 with 153 | |
# ___________________________________________________________________________(1) | |
%%timeit 5 | |
for i in range(len(df)): | |
df.at[i,"multiply_at"] = 153*df.at[i,"col1"] | |
#Time: 113 ms ± 1.53 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) | |
# ___________________________________________________________________________(2) | |
%%timeit 5 | |
df['multiply_direct'] = 153 * df["col1"] | |
#Time: 243 µs ± 4.36 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) | |
#____________________________________________________________________________(3) | |
%%timeit 5 | |
func_multiply = lambda x,y :x*y | |
df["multiply_lambda"] = func_multiply(153,df["col1"]) | |
#Time: 249 µs ± 8.09 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) | |
#____________________________________________________________________________(4) | |
%%timeit 5 | |
def multiply(row): | |
data = row[["col1"]] | |
return pd.Series({'multiply_apply': 153*data}) | |
df.apply(multiply,axis=1) | |
#Time: 10.5 s ± 129 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) | |
#____________________________________________________________________________(5) | |
%%timeit 5 | |
df['multiply_apply2'] = df.col1.apply(lambda x: 153*x) | |
#Time 3.23 ms ± 49.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment