Forked from Coldsp33d/conditional_col_creation_benchmark.py
Last active
April 25, 2021 03:53
-
-
Save Brainor/a09201fc8b398a586ab457e55ceb6cf1 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 perfplot | |
def numpy_where(df): | |
return df.assign(is_rich=np.where(df['salary'] >= 50, 'yes', 'no')) | |
def list_comp(df): | |
return df.assign(is_rich=['yes' if x >= 50 else 'no' for x in df['salary']]) | |
def loc(df): | |
df = df.assign(is_rich='no') | |
df.loc[df['salary'] > 50, 'is_rich'] = 'yes' | |
return df | |
kernels = [numpy_where, list_comp, loc] | |
df = pd.DataFrame({ | |
'age' : [21, 45, 45, 5], | |
'salary' : [20, 40, 10, 100] | |
}) | |
perfplot.show( | |
setup=lambda n: pd.concat([df] * n, ignore_index=True), | |
kernels=kernels, | |
labels=[f.__name__ for f in kernels], | |
n_range=[2**k for k in range(0, 20)], | |
logx=True, | |
logy=True, | |
xlabel='N', | |
equality_check=pd.DataFrame.equals) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment