我想要一个DataFrame,其中有开始日期和结束日期,然后使用今天的日期以%为单位计算完成时间。
DataFrame:
start-date end-date
0 01-02-2019 01-07-2022
1 03-07-2020 04-05-2022
现在我想添加列";完成%";它使用当前日期给我以%表示的完成时间
结果数据帧:
start-date end-date completion % (using 14-06-2021)
0 01-02-2019 01-07-2022 69% [(today-date)-(start-date)/(end-date)-(start-date)]
1 03-07-2020 04-05-2022 69%
df['start-date']=pd.to_datetime(df['start-date'])
df['end-date']=pd.to_datetime(df['end-date'])
尝试:
df['completion %']=((pd.Timestamp('today')-df['start-date'])/(df['end-date']-df['start-date'])*100).astype(int).astype(str)+'%'
df
:的输出
start-date end-date completion %
0 2019-01-02 2022-01-07 81%
1 2020-03-07 2022-04-05 61%
您可以通过简单的算术来实现这一点。
import pandas as pd
from datetime import datetime
df = pd.read_csv('test.csv', parse_dates=[0,1], dayfirst=True)
df['completed %'] =round((datetime.today() - df['start-date'])/(df['end-date'] - df['start-date'])*100,2)
print(df)
""" Sample Output
start-date end-date completed %
0 2019-02-01 2022-07-01 69.39
1 2020-07-03 2022-05-04 51.72
"""