Skip to content

Instantly share code, notes, and snippets.

@Attila03
Created April 11, 2018 13:20
Show Gist options
  • Save Attila03/6cd320248618378865589d03fc6e7780 to your computer and use it in GitHub Desktop.
Save Attila03/6cd320248618378865589d03fc6e7780 to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
def add_individual_rows():
heights = range(150, 211)
satisfaction_baseline = 150
d = {}
l = []
df = pd.DataFrame(columns=['height', 'satisfaction'])
for h in heights:
loc = satisfaction_baseline*((h/100.)**0.5)
size = np.random.randint(20,30)
dist = np.random.normal(loc=loc, scale=1.0, size=size)
d[h] = dist
l.append((h, dist))
for i in dist:
df = df.append({'height': h, 'satisfaction': i}, ignore_index=True)
return df
def add_multiple_rows():
heights = range(150, 211)
satisfaction_baseline = 150
d = {}
l = []
df = pd.DataFrame(columns=['height', 'satisfaction'])
for h in heights:
loc = satisfaction_baseline*((h/100.)**0.5)
size = np.random.randint(20,30)
dist = np.random.normal(loc=loc, scale=1.0, size=size)
d[h] = dist
l.append((h, dist))
rows = [(h, i) for i in dist]
temp = pd.DataFrame(rows, columns=['height', 'satisfaction'])
df = pd.concat([df, temp])
return df
%%time
x = add_individual_rows()
#Wall time: 4.23 s
%%time
y = add_multiple_rows()
#Wall time: 179 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment