I would like to zip a column of lists (in a data frame) with a fixed list. The objective is to multiply the hours, minutes, and seconds by 3600, 60, and 1 to get the total number of seconds.
Example:
import pandas as pd
df = pd.DataFrame(data = ['01:02:03','12:23:34'], columns = ['Time'])
This data looks like:
Time
0 01:02:03
1 12:23:34
I can split each of these into a list item, e.g. pd.DataFrame(df.Time.str.split(":", expand = False))
, which gives:
Time
0 [01, 02, 03]
1 [12, 23, 34]
Then I would like to zip
each of these with the list [3600, 60, 1]
, multiply the pairs, and then sum the results. I have tried to do this with the following code:
df = df.assign(Time = sum([a*b for a,b in zip([3600,60,1], [int(i) for i in df.Time.str.split(":", expand = True)])]))
But I cannot do [int(i) for i in x]
because it iterates over the pandas series df.Time.str.split(":", expand = False)
and tries to convert each list, when in fact I want the items in the list to be converted to integers. However, I'm not even sure this is the right way to zip a single list ([3600, 60, 1]
) with multiple lists.
Desired output:
Time_seconds
0 3723
1 44614
NB: in this case "time" is elapsed time, not a time of the day.