Home > OS >  Updating values using Python user input
Updating values using Python user input

Time:12-15

I have a dataset where, whenever the date value in the 'Update' column is updated, the other columns will be updated as well. Logic is:

Date1 is 3 months from Date2
Date2 is 1 month from Date3
and Update is 1 month from Date3

The only data that is changing is the dates, which are essentially getting shifted based upon the user input.

Data

Date1       Date2       Date3       Update
1/1/2021    4/1/2021    5/1/2021    6/1/2021
5           2           1           1
        

Desired

Input prompt will ask user which date value they wish to input. User inputs the date '8/1/2021', which updates the remaining column date values.

Date1       Date2       Date3       Update
3/1/2021    6/1/2021    7/1/2021    8/1/2021
5           2           1           1

Doing

I believe I can use a combination of a function as well as user prompt to approach this problem.

#take input
datevalue = input("Enter date value: ")
print(datevalue)




#use input variable in function or script to create date update
s = df['Update'].str.replace(r'(\S ) (\S )', r'\2\1')
df['Update'] = (pd.PeriodIndex(s, freq='D')   3).strftime('D%q %Y')

I am looking for some starting point suggestion or a good foundation/documentation on how to best approach this problem. I am still researching. Any suggestion is appreciated.

CodePudding user response:

Your data format is a bit messy, but this should work for you:

datevalue = pd.to_datetime(input("Enter date value: "))

df = df.T
df[1] = df[1].astype(int)
df.loc['Update', 1] = 0
df[0] = df[1].apply(lambda x: datevalue - pd.DateOffset(months=x))
df = df.T

Output:

>>> df
                 Date1                Date2                Date3               Update
0  2021-03-01 00:00:00  2021-06-01 00:00:00  2021-07-01 00:00:00  2021-08-01 00:00:00
1                    5                    2                    1                    0
  • Related