Search code examples
pythonpandastime-seriesbinning

Pandas timeseries, binning on one columnn and accumulate time spent in the bins


I have a problem regarding binning using pandas! Here is a simplified example: I have a dataframe df

     time  speed
0    0.1     20
1    0.2     20
2    0.3     30
3    0.4     30
4    0.5     40
5    0.6     40
6    0.7     40
7    0.8     40
8    0.9     50
9    1.0     50
10   1.1     60
11   1.2     70
12   1.3     80
13   1.4     80
14   1.5     80
15   1.6     80
16   1.7     40
17   1.8     40
18   1.9     40

Here is what I want to do: I would like to bin the data on speed, like bins = [0,20,40,60,80]. I would then like to have the time column values binned according to the speed-binning. After that I would like to accumulate the time in the bins and plot a histogram. Please, help


Solution

  • In [117]: binned_time = df.groupby(pd.cut(df.speed, bins=[0,20,40,60,80]))['time']                                                                                                                                                                                        
    
    In [118]: binned_time.sum()
    Out[118]: 
    speed
    (0, 20]     0.30
    (20, 40]    3.30
    (40, 60]    1.11
    (60, 80]    1.24
    Name: time, dtype: float64