Last active
February 3, 2017 17:41
-
-
Save usmcamp0811/f130a98e05dd59a7a9d2817e28091323 to your computer and use it in GitHub Desktop.
Function to quickly bin data in a pd df
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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