Home > Software design >  how to calculate total hours from dict in column pandas
how to calculate total hours from dict in column pandas

Time:09-22

I have a df column which contains dict of hours timing Example:

df['hours'][:1]
out:
         {'Monday': '0:0-0:0',
      'Tuesday': '8:0-18:30',
      'Wednesday': '8:0-18:30',
      'Thursday': '8:0-18:30',
      'Friday': '8:0-18:30',
      'Saturday': '8:0-14:0'}

by writing python code how can i calculate total hours in a week.

CodePudding user response:

Just loop through the values by using this:- for key,value in dict.items(): After this have a counter variable, counter = 0 Then for each value, split it by value.split('-')

After doing this, convert the splitted list into numbers by further splitting it with ":". After this convert both of them in minutes, then subtract them. Add this subtracted value to the counter.

You'll do this in this way.

CodePudding user response:

If you have a dictionary, you can use a python generator expression (with help of pandas.to_timedelta for the conversion):

sum((b-a).total_seconds()/3600
     for v in  d.values()
     for a,b in [[pd.to_timedelta(x ':0')
                 for x in v.split('-')]]
     )

output: 48.0

From a Series of dictionaries as strings:

def total_hours(d):
    from ast import literal_eval
    if pd.isnull(d):
        return 0
    return sum(
        (b-a).total_seconds()/3600
         for v in  literal_eval(d).values()
         for a,b in [[pd.to_timedelta(x ':0')
                     for x in v.split('-')]]
        )

df['hours'].map(total_hours)

Alternative using string methods:

(df['hours']
  .str.extractall('(?P<start>\d :\d )-(?P<stop>\d :\d )')
  .apply(lambda s: pd.to_timedelta(s ':00').dt.total_seconds().div(3600))
  .eval('stop-start').sum()
)

output:

1    48.0
dtype: float64
  • Related