简体   繁体   English

如何在 dataframe 中创建开始和结束日期

[英]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.该方法是使用pandasdatetime模块,并从提前期列中增加天数。 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.

相关问题 如何找到开始日期和结束日期给出的 dataframe 的差异 - how to find the difference of dataframe which is given on start date and end date 根据另一个 pandas 中的开始日期和结束日期列的条件创建新的 pandas dataframe - Create new pandas dataframe based on a condition on Start Date and End Date Column in another pandas Pandas即使在不同的行上,如何创建具有开始和结束的新数据帧 - Pandas How to create a new dataframe with a start and end even if on different rows Pandas:Dataframe 与每日数据的开始和结束日期 - Pandas: Dataframe with start & end date to daily data 根据数据框中的开始和结束日期更改年份 - Change year based on start and end date in dataframe Pandas,将 dataframe 转换为开始和结束日期 - Pandas, Convert dataframe to start and end date 使用开始和结束日期验证 dataframe 重复项 - Validate dataframe duplicates using start and end date 如何创建具有多个开始和结束日期的日期范围? - How to create date range with multiple start and end dates? Python:具有开始日期和结束日期的数据框,解压为 1 个日期字段 - Python: Dataframe with Start Date and End Date, Decompress to 1 Date Field 如何检查两个日期是否介于代表开始日期和结束日期的数据框的两列之间 - How to check if two dates falls between two columns of dataframe representing start date and end date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM