I have a DataFrame with column values including “time” and “location.” Example:
data = [['A', '13:00', 2], ['B', '15:00', 0], ['C', '13:00', 3], ['D', '13:00', 0], ['E', '17:00', 0]]
df = pd.DataFrame(data, columns=['location', 'time', 'other'])
I want to group the values by time and then call a function with the time and locations. I tried doing this:
result = df.groupby('time')['location'].apply(list)
But then, the times gets hidden as the indexes of result and I am unable to call the function on the time and locations in a vectorized way. It looks like this:
13:00 [A, C, D]
15:00 [B]
17:00 [E]
When I iterate through it or use apply()
I only get the location lists. Does anyone know the right way to do this?
Thank you!