How To Categorize a List

Question:

Having this list:

list_price = [['1800','5060','6300','6800','10800','3000','7100',]

how do I categorize the list to be (1000, 2000, 3000, 4000, 5000, 6000, 7000, 000)?

example:

2000: 1800
7000:6800, 6300

And count them 2000(1),7000(2), if possible using pandas as an example.

Answers:

I assumed 000 at the end of categories is 10000. Try:

cut = pd.cut(list_price, bins=(1000, 2000, 3000, 4000, 5000, 6000, 7000, 10000))
pd.Series(list_price).groupby(cut).count()
Answered By: Nuri Taş

Using rounding to the upper thousand:

list_price = ['1800','5060','6300','6800','10800','3000','7100']

out = (pd.Series(list_price).astype(int)
         .sub(1).floordiv(1000)
         .add(1).mul(100)
         .value_counts()
       )

output:

700     2
200     1
600     1
1100    1
300     1
800     1
0       1
dtype: int64

Intermediate without value_counts:

0     200
1     600
2     700
3     700
4    1100
5     300
6     800
dtype: int64
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.