Jan-31-2023, 01:23 PM
This should get you started. The key is line 7 which handles the cuts and ranges, and then sums based on that. Can use .count() instead for the count. Website is not formatting the columns quite correctly, better if you do on your own notebook.
import pandas as pd import numpy as np d = {"Sno":[1,2,3,4,5,6,7,8,9,10], "Amount":[451492,448612,451492,301492,451492,429492,451492,604492,451492,130424]} df = pd.DataFrame(data=d) df.groupby(pd.cut(df["Amount"], np.arange(0, 500000, 50000))).sum()
Output: Sno Amount
Amount
(0, 50000] 0 0
(50000, 100000] 0 0
(100000, 150000] 10 130424
(150000, 200000] 0 0
(200000, 250000] 0 0
(250000, 300000] 0 0
(300000, 350000] 4 301492
(350000, 400000] 0 0
(400000, 450000] 8 878104