Python 将列从天转换为天、小时、分钟
我正在尝试转换一列df[time\u ro\u reply] 它只包含十进制的天,而timedelta格式包含天、小时、分钟。这使它更具可读性 我正在阅读关于pd.to_timedelta的文章,但我正在努力实现它: pd.to_timedeltadf[time_to_reply]这只返回0 样本输入:Python 将列从天转换为天、小时、分钟,python,pandas,timedelta,Python,Pandas,Timedelta,我正在尝试转换一列df[time\u ro\u reply] 它只包含十进制的天,而timedelta格式包含天、小时、分钟。这使它更具可读性 我正在阅读关于pd.to_timedelta的文章,但我正在努力实现它: pd.to_timedeltadf[time_to_reply]这只返回0 样本输入: df["time_ro_reply"] 1.881551 0.903264 2.931560 2.931560 预期产出: df["time_ro_reply"] 1 days 19 hour
df["time_ro_reply"]
1.881551
0.903264
2.931560
2.931560
预期产出:
df["time_ro_reply"]
1 days 19 hours 4 minutes
0 days 23 hours 2 minutes
2 days 2 hours 23 minutes
2 days 2 hours 23 minutes
我建议按如下方式使用自定义函数:
import numpy as np
import pandas as pd
# creating the provided dataframe
df = pd.DataFrame([1.881551, 0.903264, 2.931560, 2.931560],
columns = ["time_ro_reply"])
# this function converts a time as a decimal of days into the desired format
def convert_time(time):
# calculate the days and remaining time
days, remaining = divmod(time, 1)
# calculate the hours and remaining time
hours, remaining = divmod(remaining * 24, 1)
# calculate the minutes
minutes = divmod(remaining * 60, 1)[0]
# a list of the strings, rounding the time values
strings = [str(round(days)), 'days',
str(round(hours)), 'hours',
str(round(minutes)), 'minutes']
# return the strings concatenated to a single string
return ' '.join(strings)
# add a new column to the dataframe by applying the function
# to all values of the column 'time_ro_reply' using .apply()
df["desired_output"] = df["time_ro_reply"].apply(lambda t: convert_time(t))
这将产生以下数据帧:
time_ro_reply desired_output
0 1.881551 1 days 21 hours 9 minutes
1 0.903264 0 days 21 hours 40 minutes
2 2.931560 2 days 22 hours 21 minutes
3 2.931560 2 days 22 hours 21 minutes
但是,这会产生与您描述的不同的输出。如果“time_ro_reply”值确实被解释为纯小数,我不知道您是如何得到预期结果的。你介意分享一下你是如何得到它们的吗
我希望这些注释能够很好地解释代码。如果没有,并且您不熟悉语法,例如divmod、apply,我建议您在Python/Pandas文档中查找它们
让我知道这是否有用。使用MrB显示的nice函数的修改版本 如果将time_to_reply列转换为秒并应用以下功能,也可以获得所需的输出:
import pandas as pd
df = pd.DataFrame({"time_to_reply": [1.881551, 0.903264, 2.931560, 2.931560]})
df['td_str'] = df['time_to_reply'].apply(lambda t: display_time(t*24*60*60, 3))
# df['td_str']
# 0 1 day, 21 hours, 9 minutes
# 1 0 days, 21 hours, 40 minutes
# 2 2 days, 22 hours, 21 minutes
# 3 2 days, 22 hours, 21 minutes
如果您能将示例输入与预期输出共享,那就太好了。@MayankPorwal谢谢您的反馈,我已经更新了问题。实际上,我的输入是十进制格式的天,例如3.23天……我找到了一个解决方案,但我认为已经有了一个编码函数:def days\u hours\u minutestd:return td.days,td.seconds//3600,td.seconds//60%60我不确定是否有专门用于此的编码函数。和你一样,我也得做数学题。这能回答你的问题吗?
import pandas as pd
df = pd.DataFrame({"time_to_reply": [1.881551, 0.903264, 2.931560, 2.931560]})
df['td_str'] = df['time_to_reply'].apply(lambda t: display_time(t*24*60*60, 3))
# df['td_str']
# 0 1 day, 21 hours, 9 minutes
# 1 0 days, 21 hours, 40 minutes
# 2 2 days, 22 hours, 21 minutes
# 3 2 days, 22 hours, 21 minutes