Issue
I am working on a football game dataset using python panda data. Now I have a column which is called 'gameClock', it has 'minutes : seconds' formation, like 7:52, 13:31, etc. I am trying to convert this format into seconds left for each quarter. For example, 7:52 means there is 7:08 time left for this quarter, it is equivalent to 428 seconds left for this quarter. But I have no idea how to convert these minutes:seconds time formation into all seconds left for the 900 seconds quarter. I tried the '.split()', but it returns an error like ''Series' object has no attribute 'split''. Please help me out with it. Thanks.
Solution
Convert to_timedelta
, then get the total_seconds
and rsub
to 900:
df = pd.DataFrame({'gameClock': ['7:52', '13:31']})
df['out'] = (pd.to_timedelta('00:'+df['gameClock'])
.dt.total_seconds().rsub(900)
.convert_dtypes()
)
Alternatively:
df['out'] = (pd.to_timedelta('00:'+df['gameClock'])
.rsub(pd.Timedelta('900s'))
.dt.total_seconds()
.convert_dtypes()
)
Output:
gameClock out
0 7:52 428
1 13:31 89
If you want to convert to seconds manually:
df['seconds'] = (df['gameClock']
.str.split(':', expand=True)
.astype(int)
.mul([60,1])
.sum(axis=1)
)
Output:
gameClock seconds
0 7:52 472
1 13:31 811
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.