如何使用 datetime64[ns] 和 pandas 计算天数?
How to count days using datetime64[ns] with pandas?
这里是大学生,我要为数据分析课程分析数据。
我们必须处理有关登山探险的数据集。我想计算登山者从基点到高点(expeditions['basecamp_date']
&expeditions['highpoint_date']
)用了多少天。
我找到了一种计算天数的方法,但如何将这些值放在新列中? (expeditions['days_reach_top']
)。我希望之后能够计算平均值等。
这是我现在拥有的代码:
expeditions['basecamp_date']=pd.to_datetime(expeditions['basecamp_date'])
expeditions['highpoint_date']=pd.to_datetime(expeditions['highpoint_date'])
expeditions['termination_date']=pd.to_datetime(expeditions['termination_date'])
date_format = "%Y-%m-%d/ns"
a = datetime.strptime(str(expeditions['basecamp_date']), date_format)
b = datetime.strptime(str(expeditions['highpoint_date']), date_format)
delta = b - a
print (delta.days)
expeditions['days_reach_top']
我知道它不完整,但它给了我 ValueError("unconverted data remains: %s" %
和 ValueError("time data %r does not match format %r" %
。如何修复时间格式并用它进行微积分?
对于上下文,这是我的数据集的样子:
感谢您的帮助!
尝试替换
a = datetime.strptime(str(expeditions['basecamp_date']), date_format)
b = datetime.strptime(str(expeditions['highpoint_date']), date_format)
和
a = expeditions['basecamp_date']
b = expeditions['highpoint_date']
这里是大学生,我要为数据分析课程分析数据。
我们必须处理有关登山探险的数据集。我想计算登山者从基点到高点(expeditions['basecamp_date']
&expeditions['highpoint_date']
)用了多少天。
我找到了一种计算天数的方法,但如何将这些值放在新列中? (expeditions['days_reach_top']
)。我希望之后能够计算平均值等。
这是我现在拥有的代码:
expeditions['basecamp_date']=pd.to_datetime(expeditions['basecamp_date'])
expeditions['highpoint_date']=pd.to_datetime(expeditions['highpoint_date'])
expeditions['termination_date']=pd.to_datetime(expeditions['termination_date'])
date_format = "%Y-%m-%d/ns"
a = datetime.strptime(str(expeditions['basecamp_date']), date_format)
b = datetime.strptime(str(expeditions['highpoint_date']), date_format)
delta = b - a
print (delta.days)
expeditions['days_reach_top']
我知道它不完整,但它给了我 ValueError("unconverted data remains: %s" %
和 ValueError("time data %r does not match format %r" %
。如何修复时间格式并用它进行微积分?
对于上下文,这是我的数据集的样子:
感谢您的帮助!
尝试替换
a = datetime.strptime(str(expeditions['basecamp_date']), date_format)
b = datetime.strptime(str(expeditions['highpoint_date']), date_format)
和
a = expeditions['basecamp_date']
b = expeditions['highpoint_date']