This is what my dataframe looks like:
force_min force_max
name local_dttm
machine_a 2021-01-06 16:40:00 420 700
2021-01-06 16:50:00 1010 1500
2021-01-06 17:00:00 950 975
....
2021-07-21 12:00:00 100 699
2021-07-21 12:10:00 450 940
2021-07-21 12:20:00 805 1800
I would like to bin the data on force_min ( bins = [0,200,400,600,800,1000,1200])
Then I would like to sum the time spent in the force_min bins.
Notes:
- force_min and force_max are measured every 10 minutes as noted in local_dttm
- name and local_dttm are the index
Ideally I would create a new dataframe that looks like this:
bins time_spent_force_min
(0, 200] 00:10:00
(200, 400] 00:00:00
(400, 600] 00:20:00
(600, 800] 00:00:00
(800, 1000] 00:20:00
(1000, 1200] 00:10:00
This is what I've tried so far:
time_spent = df.groupby(pd.cut(df["force_min"], bins=[0,200,400,600,800,1000,1200]))["local_dttm"]
it returns KeyError: 'Column not found: local_dttm'
If I convert the index to columns and try this now
time_spent = df.groupby(pd.cut(df["force_min"], bins=[0,200,400,600,800,1000,1200]))["local_dttm"]
time_spent.sum()
it returns AssertionError: result has not been initialized.
I believe this is because local_dttm is a timestamp.
What should I do? I am lost.