I have a data frame representing the schedule of some restaurants in a week.
- What i want to do is to add a column
week_hours
to my initial Dataframedf
that represents the total number of hours the restaurant is open per week.
note : the value 0 simply means that the restaurant is closed that day.
data = {
'restaurant_id': ['1', '2','3'],
'Monday': ['11:0-20:0', '11:30-22:0','11:30-21:0'],
'Tuesday': ['11:0-20:0', '11:30-22:0','11:30-22:0'],
'Wednesday': ['11:0-20:0', '11:30-22:0','11:30-21:0'],
'Thursday': ['11:0-20:0', '11:30-22:0','11:30-21:0'],
'Friday': ['11:0-22:0', '11:30-22:0','11:30-21:0'],
'Saturday': ['11:0-22:0', '12:0-22:0','0'],
'Sunday': ['11:0-17:0', '16:30-21:30','11:30-21:0',],
}
df = pd.DataFrame (data, columns = ['restaurant_id','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday',])
df.head()
What could be a simple syntax to achieve this?
One way is to first
transpose
and then create custom function toextract
the values before calculation (note that this works only if the hours doesn't go pass 24, else needTimedelta
):