I have a dataframe with data of customers entering and leaving a building, see example here:
import pandas as pd
from datetime import datetime, timedelta
data = {'customer': ['nameA', 'nameA', 'nameB', 'nameC', 'nameB', 'nameB', 'nameD', 'nameC', 'nameD', 'nameB'],
'event': ['in', 'out', 'in', 'in', 'out', 'in', 'in', 'out', 'out', 'out',],
'time_stamp': ['2020-04-20 09:58:47', '2020-04-20 19:58:52', '2020-04-20 09:28:52',
'2020-04-20 10:18:52', '2020-04-20 09:58:52', '2020-04-20 11:08:52',
'2020-04-20 13:58:52', '2020-04-20 14:58:52', '2020-04-20 15:58:52',
'2020-04-20 19:58:52']}
Is there a good way to calculate the time spent in the building for each person?
The problems I have is that some people enter the buildings more often, which makes it more difficult to handle. So far I split the dataframe into two separate ones 'in' and 'out' based on the corresponding event, delete all duplicates (so people who enter the building multiple times are ignored completely), sort both dataframes by customer and calculate the time difference between the dataframes 'in' and 'out'.
Is there a better way to do it? I was thinking iterating through the dataframes, but I think you should avoid it for time reasons if you have large data sets...
Thanks for your ideas!