递增数字递减日期列

Increasing Number Decreasing Date Column

需要帮助,我正在尝试根据用户输入获取日期列,它会随着总年龄列数的增加以一个月为间隔减少当前日期。

CurrentDf = {
  "VIN": ['v1','v1','v1','v2','v2','v2','v2','v3','v3','v3'],
  "Total Age": [10,10,10, 08,08,08,08,11,11,11],
  "Months": [05,07,09,01,06,07,08,02,05,07],
  "Monthly Revenue": [1108,4330,7121,1998,1783,9628, 2082,8763,5683, 5780]
}

Total_Age = [int(x) for x in input('Please enter Total Age?= ').split(',')]

当前日期:

VIN Total Age Months Monthly Revenue
v1 10 05 1108
v1 10 07 4330
v1 10 09 7121
v2 08 01 1998
v2 08 06 1783
v2 08 07 9628
v2 08 08 2082
v3 11 02 8763
v3 11 05 5683
v3 11 07 5780

用户输入:[8]

输出DF:

VIN Total Age Months Monthly Revenue Date
v1 10 05 1108 2022-04-01
v1 10 07 4330 2022-04-01
v1 10 09 7121 2022-04-01
v2 08 01 1998 2022-05-01
v2 08 06 1783 2022-05-01
v2 08 07 9628 2022-05-01
v2 08 08 2082 2022-05-01
v3 11 02 8763 2022-03-01
v3 11 05 5683 2022-03-01
v3 11 07 5780 2022-03-01

如果我理解正确,请尝试对年龄进行排名以产生所需的月份偏移量:

input_age = int(input('Please enter Total Age?= '))

df["Date"] = df["Total Age"].astype(int).sub(input_age).rank(method='dense').apply(lambda x: pd.Timestamp.today().date()-pd.DateOffset(months=x-1))
input_age = 8 的输出:
>>> df
  VIN Total Age Months  Monthly Revenue       Date
0  v1        10     05             1108 2022-04-01
1  v1        10     07             4330 2022-04-01
2  v1        10     09             7121 2022-04-01
3  v2        08     01             1998 2022-05-01
4  v2        08     06             1783 2022-05-01
5  v2        08     07             9628 2022-05-01
6  v2        08     08             2082 2022-05-01
7  v3        11     02             8763 2022-03-01
8  v3        11     05             5683 2022-03-01
9  v3        11     07             5780 2022-03-01
输入 df:
df = pd.DataFrame({"VIN": ['v1','v1','v1','v2','v2','v2','v2','v3','v3','v3'],
                   "Total Age": ['10','10','10','08','08','08','08','11','11','11'],
                   "Months": ['05','07','09','01','06','07','08','02','05','07'],
                   "Monthly Revenue": [1108,4330,7121,1998,1783,9628, 2082,8763,5683, 5780]
                   })