cut() function in Python does not work like cut() function in R

Viewed 237

In R, we can use cut() like this

e=c(4,5,6,7,8,9,10)

cut(e, breaks = c(1,2,3,4,5,6,7,8,9,10),
              include.lowest = TRUE,
              dig.lab = 3,
              right = FALSE)

the output is:

[4,5)  [5,6)  [6,7)  [7,8)  [8,9)  [9,10] [9,10]
Levels: [1,2) [2,3) [3,4) [4,5) [5,6) [6,7) [7,8) [8,9) [9,10]

In Python, I use this:

e=np.r_[4,5,6,7,8]

pd.cut(e, bins=np.r_[1,2,3,4,5,6,7,8,9,10],right = False, include_lowest =True )

The output is:

[[4.0, 5.0), [5.0, 6.0), [6.0, 7.0), [7.0, 8.0), [8.0, 9.0), [9.0, 10.0), NaN]
Categories (9, interval[int64]): [[1, 2) < [2, 3) < [3, 4) < [4, 5) ... [6, 7) < 
[7, 8) < [8, 9) < [9, 10)]

1.Why there is a different in "[9,10]" in R's to "[9.0, 10.0), NaN" in Python's?

2.How can I get the same result in Python that turns "[9.0, 10.0), NaN" to [9,10] like R's?

3.Because if I use cut() in R, I can have the value like example the value "10", but the cut() in Python because of [9.0, 10.0) so that I can't get 10.0 . If I set right= Wrong, I can have (9.0, 10.0] but the fist one will be (4.0, 5.0] so that I can't get the value 4.0 either. Please help me so that I can have both 4.0 and 10.0 in the result.

Thanks so much.

0 Answers
Related