Skip to content

Instantly share code, notes, and snippets.

@naenumtou
Created July 27, 2020 06:51
Show Gist options
  • Save naenumtou/cd7434b27a34ac67671e3fcd64f9565c to your computer and use it in GitHub Desktop.
Save naenumtou/cd7434b27a34ac67671e3fcd64f9565c to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
import time
#Mock data
data = np.random.randint(0,100,size = (2**20, 1))
df = pd.DataFrame(data, columns = ['test'])
#Pandas apply
s = time.time()
def checking(df):
if df['test'] < 50:
flag = 1
else:
flag = 0
return flag
df['apply_1'] = df.apply(checking, axis = 1)
e = time.time()
print('Apply 1 time = {}'.format(e-s))
s = time.time()
def checking2(df):
if df['test'] < 30:
flag = 1
elif df['test'] < 50:
flag = 2
else:
flag = 3
return flag
df['apply_2'] = df.apply(checking2, axis = 1)
e = time.time()
print('Apply 2 time = {}'.format(e-s))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment