简体   繁体   English

如何将 int64 转换为分钟? 蟒蛇熊猫

[英]How convert an int64 to minutes ? Python Pandas

I would like to transform this column (format int64) to a minutes format to do calculation我想将此列(格式为 int64)转换为分钟格式以进行计算

Duration(min)
10
30
5
15
5

I tried this:我试过这个:

df['Duration(min)'] = pd.to_datetime(df['Duration(min)'], format='%H%M')

But I have ValueError: time data '5' does not match format '%H%M' (match)但我有ValueError: time data '5' does not match format '%H%M' (match)

You need to use pd.to_timedelta here.您需要在此处使用pd.to_timedelta

pd.to_timedelta(df['Duration(min)'], unit='min')

0   0 days 00:10:00
1   0 days 00:30:00
2   0 days 00:05:00
3   0 days 00:15:00
4   0 days 00:05:00
Name: Duration(min), dtype: timedelta64[ns]

unit -> 'm' / 'minute' / 'min' / 'minutes' / 'T' all are shorthand for minutes . unit -> 'm' / 'minute' / 'min' / 'minutes' / 'T'都是分钟的简写。

Using pd.to_datetime with strftime -使用pd.to_datetimestrftime -

pd.to_datetime(df.Duration(min), unit='m').dt.strftime('%H:%M')

Output-输出-

0    00:10
1    00:30
2    00:05
3    00:15
4    00:05
dtype: object

Why %H%M when all you have is minutes?为什么 %H%M 当你只有几分钟? Try format='%M'.试试格式='%M'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM