[英]how to create start and end date in dataframe
Product component lead time
mobile batteries 2days
mobile charger 4 days
charger cable 2 days
charger adapter 2 days
I want to create a start date and the due date for example adapter start date should be started by today and due date =start date+ lead time.我想创建一个开始日期和截止日期,例如适配器开始日期应该从今天开始,截止日期=开始日期+提前期。 for product mobile and component charger's start date is equal to the due date of the charger cable how can I implement this logic in python对于产品移动和组件充电器的开始日期等于充电器电缆的到期日期如何在 python 中实现此逻辑
The approach would be to use the pandas
and datetime
module and increment the number of days from the lead time column.该方法是使用pandas
和datetime
模块,并从提前期列中增加天数。 But to use the number of days as a number, one would have to extract it out from the string (by removing the 'days', removing all whitespaces and converting the resultant to integer).但是要将天数用作数字,必须从字符串中提取它(通过删除“天”,删除所有空格并将结果转换为整数)。 Following is the implementation in code!以下是代码中的实现!
import pandas as pd
import datetime
d = {
"Product":['mobile', 'mobile', 'charger', 'charger'],
"component":['batteries', 'charger', 'cable', 'adapter'],
"lead time":['2days', '4 days', '2 days', '2 days']
}
df = pd.DataFrame(data=d)
df['start date'] = datetime.datetime.now().date()
df['due date'] = ''
for i in range(0,len(df)):
n = int(((df['lead time'][i]).replace("days","")).strip())
df['due date'][i] = datetime.datetime.now().date()+datetime.timedelta(days=n)
The resulting dataframe would be -生成的 dataframe 将是 -
Product component lead time start date due date
0 mobile batteries 2days 2021-03-12 2021-03-14
1 mobile charger 4 days 2021-03-12 2021-03-16
2 charger cable 2 days 2021-03-12 2021-03-14
3 charger adapter 2 days 2021-03-12 2021-03-14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.