Python Answers

How to bin a column with Python Pandas?

Spread the love

To bin a column with Python Pandas, we can use the cut method.

For instance, we werite

bins = [0, 1, 5, 10, 25, 50, 100]
df['binned'] = pd.cut(df['percentage'], bins)

to add the binned column by calling cut with the data frame df‘s percentage column with the bins.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *