0

I have a df that looks like,

ID | Time_A | Time_B
B     5.0      3.5
C     3.0      4.0
A     2.5      1.0

I want to create a new column that classifies Time_B column into Bins of 0-5, 5-10, 10-15, 15-20, and 20 - anything larger. Where none of the bins have overlapping values, ( where 5 does not exist in 5-10 if it exists in 0-5.

Thanks!

Chris90
  • 1,868
  • 5
  • 20
  • 42
  • 1
    you are looking at `pd.cut(df.Time_B, bins=[0,5,10,15,20,np.inf], include_lowest=True)`. – Quang Hoang Jun 19 '19 at 19:43
  • thanks!, what does the include lowest do? does it mean each bin will include the lowest? but I did not want overlap in any value for the bins @QuangHoang – Chris90 Jun 19 '19 at 19:47
  • 1
    `include_lowest` will include the `0` value to your lowest bin. Otherwise, a `0` value would give a `NaN` cut. If you don't mind this or don't have `0` in the data, then you can ignore this option. – Quang Hoang Jun 19 '19 at 19:48
  • Thanks, so for each Bin the highest value for that bin which is the lowest value in next bin will only appear in the bin where it is the highest correct? @QuangHoang – Chris90 Jun 19 '19 at 19:55
  • 1
    Yes, there a `closed` option for `qcut` which defaults to `'right'`. – Quang Hoang Jun 19 '19 at 19:56
  • what is the difference between qcut just cut as you showed above? sorry @QuangHoang – Chris90 Jun 19 '19 at 20:03
  • Thanks @QuangHoang so if I wanted to do that, just replace the pd.cut with pd.qcut and add that closed parameter? – Chris90 Jun 19 '19 at 20:13
  • 1
    Oh, I missed that. `qcut` divides your columns into (almost) equal size, while `cut` divides your column by values. So you definitely want `cut` here. – Quang Hoang Jun 19 '19 at 20:15
  • ahh, got it so use that closed parameter with cut not qcut? @QuangHoang – Chris90 Jun 19 '19 at 20:18
  • 1
    Yes, I wasn't thinking properly. `qcut` has nothing to do with your question. – Quang Hoang Jun 19 '19 at 20:19
  • @QuangHoang Thanks, so you mentioned just use the closed option under the same cut method ? thanks! – Chris90 Jun 19 '19 at 20:36
  • 1
    Yes, but you want the default behavior, so you don't need to do anything. – Quang Hoang Jun 19 '19 at 20:37
  • Thanks @QuangHoang – Chris90 Jun 19 '19 at 20:58

0 Answers0