Skip to content

Instantly share code, notes, and snippets.

@usmcamp0811
Last active February 3, 2017 17:41
Show Gist options
  • Save usmcamp0811/f130a98e05dd59a7a9d2817e28091323 to your computer and use it in GitHub Desktop.
Save usmcamp0811/f130a98e05dd59a7a9d2817e28091323 to your computer and use it in GitHub Desktop.
Function to quickly bin data in a pd df
import pandas as pd
import numpy as np
def bin_data(dataframe, field, num_bins):
bins = np.linspace(df[field].min(), df[field].max(), num_bins)
dataframe[field+'_Bins'] = pd.cut(dataframe[field], bins)
return dataframe
if __name__ == "__main__":
df = pd.DataFrame(np.random.uniform(0, 100, size=(100, 3)))
df.columns = ['A', 'B', 'C']
print(bin_data(df, 'C', 5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment