我有一个Pandas数据框,其中的时间列为字符串格式,如下所示:

 id       playTime

 12       1d 13h 23m
 13      42d 22h 47m
 14       7d 16h 31m
 15        1h 26m 6s
 16           9d 58m


tried pd.to_datetime(df['playTime'], format='%j%H%M%S')但它显示time data does not match format '%j%H%M%S' (match). In the end, I'd like to convert the df ['playTime'] values to seconds.

最佳答案

我认为需要to_timedelta用于Timedeltatotal_seconds

df['playTime'] = pd.to_timedelta(df['playTime']).dt.total_seconds().astype(int)
print (df)
   id  playTime
0  12    134580
1  13   3710820
2  14    664260
3  15      5166
4  16    781080

10-06 05:22