Skip to content

Instantly share code, notes, and snippets.

@FBosler
Created October 26, 2020 17:02
Show Gist options
  • Save FBosler/def6d362e7f210716b052c552cb6e5d6 to your computer and use it in GitHub Desktop.
Save FBosler/def6d362e7f210716b052c552cb6e5d6 to your computer and use it in GitHub Desktop.
Apply / Lambda
import numpy as np
import pandas as pd
samples = 100000
df = pd.DataFrame(
{
'col1':np.random.randint(2,100,samples),
'col2':np.random.randint(2,100,samples),
'col3':np.random.rand(samples)
}
)
df.col3 = df.col3.apply(lambda x: str(x).replace('.',','))
%%timeit
results = []
for _, row in df.iterrows():
results.append(float(row.col3.replace(',','.')))
%%timeit
results = df.col3.apply(lambda x: x.replace(',','.')).values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment