1

I am trying to take receive data, bin the data, and then output the data. I am doing so using pandas.cut. I am looking to alter my code so the bounds produced by pandas.cut are whole numbers. Below is my current code:

for (ColumnName, columnData) in df.iteritems():
    df[ColumnName+'_binned']=pd.cut(df[ColumnName],number_of_bins))
    #cutting each row into bins 

I would like the bins to only have whole numbers instead of decimals. For example: the current output is 1.75-5.33. I would like this result to be rounded to 2 - 6. What is the best way to alter my current code? Is this possible? Thank you in advance.

Dah la la
  • 99
  • 6
  • Check [this](https://stackoverflow.com/questions/32552027/with-pandas-cut-how-do-i-get-integer-bins-and-avoid-getting-a-negative-lowe) out. – Mayank Porwal Dec 03 '20 at 18:44
  • Is there any workaround to this? this affects multiple columns so struggling to loop the attached suggestion over multiple columns. – Dah la la Dec 03 '20 at 20:15

1 Answers1

2

This is the following code I used to fix this previously answered question:

for (ColumnName, columnData) in df.iteritems():
    df[ColumnName+'_binned']=pd.cut(df[ColumnName],bin_count, precision=0)

Setting the precision to zero rounded all the answers. From the previously issued problem, it returns answers such as 2.0-6.0.

Dah la la
  • 99
  • 6