I have the follwing DataFrame
import pandas as pd
data = {"hours": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
"values": [0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1]}
df = pd.DataFrame(data)
I have been trying to add an extra column to df
including the values by groupby values
and the follwing list:
[2, 4, 6, 8, 10, 16, 18, 21, 23]
this list represents hours after which the gruoping should be conducted. E.g. in the new column category
it gives 1 for those values
between 2 and 4 gives 1 and else where gives 0 and for hours between 6 and 8 gives 2 where the values are 1 and else where 0 and so on..
I tried the following:
df.groupby(["values", "hours"])
and I could not come forward with it.
The expected result looks like:
question from:
https://stackoverflow.com/questions/66060998/groupby-and-lists-category