Pandas - 将 HH:MM:SS.F 字符串转换为秒 - 注意:HH 有时超过 24H
Pandas - Convert HH:MM:SS.F string to seconds - Caveat : HH sometimes goes over 24H
我有以下数据框:
**flashtalking_df =**
+--------------+--------------------------+------------------------+
| Placement ID | Average Interaction Time | Total Interaction Time |
+--------------+--------------------------+------------------------+
| 2041083 | 00:01:04.12182 | 24:29:27.500 |
| 2041083 | 00:00:54.75043 | 52:31:48.89108 |
+--------------+--------------------------+------------------------+
其中 00:01:04.12182 = HH:MM:SS.F
我需要将 Average Interaction Time 和 Total Interaction Time 这两个列都转换为秒数。
问题是总交互时间超过 24 小时。
我发现以下代码大部分都有效。但是,当总交互时间超过 24 小时时,它给了我
ValueError: time data '24:29:27.500' does not match format '%H:%M:%S.%f'
这是我目前正在使用的函数,它是我从另一个 Stack Overflow 问题中获取的,用于平均交互时间和总交互时间:
flashtalking_df['time'] = flashtalking_df['Total Interaction Time'].apply(lambda x: datetime.datetime.strptime(x,'%H:%M:%S.%f'))
flashtalking_df['timedelta'] = flashtalking_df['time'] - datetime.datetime.strptime('00:00:00.00000','%H:%M:%S.%f')
flashtalking_df['Total Interaction Time'] = flashtalking_df['timedelta'].apply(lambda x: x / np.timedelta64(1, 's'))
如果有更简单的方法,请告诉我。
感谢大家的帮助
我认为你需要先转换 to_timedelta
and then to seconds
by astype
:
df['Average Interaction Time'] = pd.to_timedelta(df['Average Interaction Time'])
.astype('timedelta64[s]')
.astype(int)
df['Total Interaction Time'] = pd.to_timedelta(df['Total Interaction Time'])
.astype('timedelta64[s]')
.astype(int)
.map('{:,.2f}'.format)
print (df)
Placement ID Average Interaction Time Total Interaction Time
0 2041083 64 88,167.00
1 2041083 54 189,108.00
total_seconds
, thank you 的解决方案:
df['Average Interaction Time'] = pd.to_timedelta(df['Average Interaction Time'])
.dt.total_seconds()
.map('{:,.2f}'.format)
df['Total Interaction Time'] = pd.to_timedelta(df['Total Interaction Time'])
.dt.total_seconds()
.map('{:,.2f}'.format)
print (df)
Placement ID Average Interaction Time Total Interaction Time
0 2041083 64.12 88,167.50
1 2041083 54.75 189,108.89
我有以下数据框:
**flashtalking_df =**
+--------------+--------------------------+------------------------+
| Placement ID | Average Interaction Time | Total Interaction Time |
+--------------+--------------------------+------------------------+
| 2041083 | 00:01:04.12182 | 24:29:27.500 |
| 2041083 | 00:00:54.75043 | 52:31:48.89108 |
+--------------+--------------------------+------------------------+
其中 00:01:04.12182 = HH:MM:SS.F
我需要将 Average Interaction Time 和 Total Interaction Time 这两个列都转换为秒数。
问题是总交互时间超过 24 小时。
我发现以下代码大部分都有效。但是,当总交互时间超过 24 小时时,它给了我
ValueError: time data '24:29:27.500' does not match format '%H:%M:%S.%f'
这是我目前正在使用的函数,它是我从另一个 Stack Overflow 问题中获取的,用于平均交互时间和总交互时间:
flashtalking_df['time'] = flashtalking_df['Total Interaction Time'].apply(lambda x: datetime.datetime.strptime(x,'%H:%M:%S.%f'))
flashtalking_df['timedelta'] = flashtalking_df['time'] - datetime.datetime.strptime('00:00:00.00000','%H:%M:%S.%f')
flashtalking_df['Total Interaction Time'] = flashtalking_df['timedelta'].apply(lambda x: x / np.timedelta64(1, 's'))
如果有更简单的方法,请告诉我。
感谢大家的帮助
我认为你需要先转换 to_timedelta
and then to seconds
by astype
:
df['Average Interaction Time'] = pd.to_timedelta(df['Average Interaction Time'])
.astype('timedelta64[s]')
.astype(int)
df['Total Interaction Time'] = pd.to_timedelta(df['Total Interaction Time'])
.astype('timedelta64[s]')
.astype(int)
.map('{:,.2f}'.format)
print (df)
Placement ID Average Interaction Time Total Interaction Time
0 2041083 64 88,167.00
1 2041083 54 189,108.00
total_seconds
, thank you
df['Average Interaction Time'] = pd.to_timedelta(df['Average Interaction Time'])
.dt.total_seconds()
.map('{:,.2f}'.format)
df['Total Interaction Time'] = pd.to_timedelta(df['Total Interaction Time'])
.dt.total_seconds()
.map('{:,.2f}'.format)
print (df)
Placement ID Average Interaction Time Total Interaction Time
0 2041083 64.12 88,167.50
1 2041083 54.75 189,108.89