利用 Python Input() 对数据集执行 DateShift
Utilize Python Input() to perform DateShift on a dataset
我有一个数据集,每当 Update
列中的日期值输入到 input() 提示符时,Date1、Date2 和 日期 3
值将根据这些规则更新:
Date1 is 5 months from the date entered in the input() prompt
Date2 is 2 months from the date entered in the input() prompt
Date3 is 1 month from the date entered in the input() prompt
唯一发生变化的数据是 日期,它们实际上是根据用户输入而改变的。
数据
ID Stat Date1 Date2 Date3 Update
aa 500 1/1/2021 4/1/2021 5/1/2021 6/1/2021
bb 800 1/1/2021 4/1/2021 5/1/2021 6/1/2021
需要
输入提示会询问用户他们希望输入哪个日期值。
用户输入日期“8/1/2021”,这会更新剩余的列日期值。 根据上述规则。
ID Stat Date1 Date2 Date3 Update
aa 500 3/1/2021 6/1/2021 7/1/2021 8/1/2021
bb 800 3/1/2021 6/1/2021 7/1/2021 8/1/2021
正在做
我相信我可以结合使用函数和用户提示来解决这个问题。
类型输入
datevalue = pd.to_datetime(input("Enter date value: "))
print(datevalue)
在函数或脚本中使用输入变量为 Date1、Date2 和 Date3 创建日期更新
df[0] = df[1].apply(lambda x: datevalue - pd.DateOffset(months=x))
s = df['Update'].str.replace(r'(\S+) (\S+)', r'')
df['Update'] = (pd.PeriodIndex(s, freq='D') + 3).strftime('D%q %Y')
我正在寻找 一些关于如何最好地解决此问题的起点建议或好的 foundation/documentation。我还在研究。任何建议表示赞赏。
我想你正在寻找这样的东西:
cols = df.filter(like='Date').columns.tolist() + ['Update']
df[cols] = df[cols].apply(lambda col: pd.to_datetime(col) + (datevalue - df['Update']))
输出:
>>> df
ID Stat Date1 Date2 Date3 Update
0 aa 500 2021-03-03 2021-06-01 2021-07-01 2021-08-01
1 bb 800 2021-03-03 2021-06-01 2021-07-01 2021-08-01
我们可以定义一个映射字典,将列名映射到相应的日期偏移量,然后从这个字典创建一个系列,并从偏移量中减去 datevalue
,最后 assign
更新的日期值返回到数据框
d = {
'Date1': pd.DateOffset(months=5),
'Date2': pd.DateOffset(months=2),
'Date3': pd.DateOffset(months=1),
}
s = pd.Series(d).rsub(datevalue)
df.assign(**{**s, 'Update': datevalue})
ID Stat Date1 Date2 Date3 Update
0 aa 500 2021-03-01 2021-06-01 2021-07-01 2021-08-01
1 bb 800 2021-03-01 2021-06-01 2021-07-01 2021-08-01
我有一个数据集,每当 Update
列中的日期值输入到 input() 提示符时,Date1、Date2 和 日期 3
值将根据这些规则更新:
Date1 is 5 months from the date entered in the input() prompt
Date2 is 2 months from the date entered in the input() prompt
Date3 is 1 month from the date entered in the input() prompt
唯一发生变化的数据是 日期,它们实际上是根据用户输入而改变的。
数据
ID Stat Date1 Date2 Date3 Update
aa 500 1/1/2021 4/1/2021 5/1/2021 6/1/2021
bb 800 1/1/2021 4/1/2021 5/1/2021 6/1/2021
需要
输入提示会询问用户他们希望输入哪个日期值。 用户输入日期“8/1/2021”,这会更新剩余的列日期值。 根据上述规则。
ID Stat Date1 Date2 Date3 Update
aa 500 3/1/2021 6/1/2021 7/1/2021 8/1/2021
bb 800 3/1/2021 6/1/2021 7/1/2021 8/1/2021
正在做
我相信我可以结合使用函数和用户提示来解决这个问题。
类型输入
datevalue = pd.to_datetime(input("Enter date value: "))
print(datevalue)
在函数或脚本中使用输入变量为 Date1、Date2 和 Date3 创建日期更新
df[0] = df[1].apply(lambda x: datevalue - pd.DateOffset(months=x))
s = df['Update'].str.replace(r'(\S+) (\S+)', r'')
df['Update'] = (pd.PeriodIndex(s, freq='D') + 3).strftime('D%q %Y')
我正在寻找 一些关于如何最好地解决此问题的起点建议或好的 foundation/documentation。我还在研究。任何建议表示赞赏。
我想你正在寻找这样的东西:
cols = df.filter(like='Date').columns.tolist() + ['Update']
df[cols] = df[cols].apply(lambda col: pd.to_datetime(col) + (datevalue - df['Update']))
输出:
>>> df
ID Stat Date1 Date2 Date3 Update
0 aa 500 2021-03-03 2021-06-01 2021-07-01 2021-08-01
1 bb 800 2021-03-03 2021-06-01 2021-07-01 2021-08-01
我们可以定义一个映射字典,将列名映射到相应的日期偏移量,然后从这个字典创建一个系列,并从偏移量中减去 datevalue
,最后 assign
更新的日期值返回到数据框
d = {
'Date1': pd.DateOffset(months=5),
'Date2': pd.DateOffset(months=2),
'Date3': pd.DateOffset(months=1),
}
s = pd.Series(d).rsub(datevalue)
df.assign(**{**s, 'Update': datevalue})
ID Stat Date1 Date2 Date3 Update
0 aa 500 2021-03-01 2021-06-01 2021-07-01 2021-08-01
1 bb 800 2021-03-01 2021-06-01 2021-07-01 2021-08-01