Skip to content

Instantly share code, notes, and snippets.

@messiest
Created December 20, 2017 18:34
Show Gist options
  • Save messiest/c168d77623c9b59a4c47e144db5a4368 to your computer and use it in GitHub Desktop.
Save messiest/c168d77623c9b59a4c47e144db5a4368 to your computer and use it in GitHub Desktop.
bootstrap samples
def bootstrap(df, n, to_df=True):
"""
generate n bootstraped samples from a DataFrame
"""
assert isinstance(df, type(pd.DataFrame())),\
f"Expected pandas.DataFrame, got type: {type(df)}"
sample = {column: np.random.choice(df[column], size=int(n)) for column in df.columns} # column: bootstrap sample
if to_df: sample = pd.DataFrame.from_dict(sample) # convert to DataFrame
return sample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment