Home > Blockchain >  How To Categorize a List
How To Categorize a List

Time:08-26

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.

CodePudding user response:

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

CodePudding user response:

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()
  • Related