1

I have the following series:

0         79.0
1        220.0
2        185.0
3        199.0
4        226.0
5        141.0
6        341.0
7        151.0
8         57.0
9        313.0
10       273.0
11       113.0
12       328.0

If i use pandas.cut() on this, this is what i get:

    series  equal_intvls
0   79.0    (0.979, 306.1]
1   220.0   (0.979, 306.1]
2   185.0   (0.979, 306.1]
3   199.0   (0.979, 306.1]
4   226.0   (0.979, 306.1]
5   141.0   (0.979, 306.1]
6   341.0   (306.1, 608.2]
7   151.0   (0.979, 306.1]
8   57.0    (0.979, 306.1]
9   313.0   (306.1, 608.2]
10  273.0   (0.979, 306.1]
11  113.0   (0.979, 306.1]
12  328.0   (306.1, 608.2]

pandas.cut() is giving me a series of intervals which have the same length (max value - min value), the length of the intervals is 2, but from the start point on the intervals till the end point there are several numbers within each interval that may not be the same for each of the intervals.

If i use pandas.cut() i get intervals of the same length, but how could i split this series into intervals that contain the same number of elements in each interval??

What i would like to obtain is a new column containing these intervals with the same number of elements within them. Taking as an example the following array:

[1, 7, 7, 4, 6, 3]

what i would like to obtain is this series of intervals with the same number of items:

[(0.999, 3.667] ,(3.667, 6.333] , (6.333, 7.0]]


(0.999, 3.667] - There are 2 values in this imterval: (1, 3)
(3.667, 6.333] - There are 2 values in this interval (4, 6)
(6.333, 7.0] - And again, 2 values within this interval (7, 7)

I would like to get the intervals in a series-like form so i can input it as a new column into y original df.

I have tried np.split, and np.array_split without success, i have also visited some other posts in this website that are similar to what i want but non seems to really fit my case. Please help.

What's the best way to get these kinds of intervals??

Thank you very much in advance

1 Answer 1

1

I think You are looking for qcut:

>>> >>> pd.qcut(pd.Series([1, 7, 7, 4, 6, 3]),3)
0    (0.999, 3.667]
1      (6.333, 7.0]
2      (6.333, 7.0]
3    (3.667, 6.333]
4    (3.667, 6.333]
5    (0.999, 3.667]
dtype: category
Categories (3, interval[float64]): [(0.999, 3.667] < (3.667, 6.333] < (6.333, 7.0]]
1
  • 1
    damn it was so simple?? i was driving myself crazy looking for a functionality that could do this for me. Thank you very much!! Dec 9, 2018 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.