1

I have a dataset where I would like a certain statement to only be applied to specific rows in my dataset. I would like this to only be applied to rows that contain [type] == 'aa', and apply second statement for rows that contain [type] == 'bb'

Data

location    type    mig1        de          mig2        re
ny          aa      8/1/2021    10/1/2021   1/1/2022    2/1/2022
ny          aa      8/1/2021    10/1/2021   1/1/2022    2/1/2022
ca          aa      8/1/2021    10/1/2021   1/1/2022    2/1/2022
tx          bb      9/1/2021    11/1/2021   2/1/2022    3/1/2022

Desired

 #Date 8/1/2022 is input, which yields:

    location    type    mig1        de          mig2        re
    ny          aa      03/1/2022   05/1/2022   8/1/2022    7/1/2022
    ny          aa      03/1/2022   05/1/2022   8/1/2022    7/1/2022
    ca          aa      03/1/2022   05/1/2022   8/1/2022    7/1/2022
    tx          bb      03/1/2021   11/1/2021   08/1/2022   3/1/2022



Logic - we see that the shift applies to all columns when the type == aa
        when the type value == bb, a different statement is applied

Doing

#takes input value

datevalue = pd.to_datetime(input("Enter shift: "))

#shifts dates from the datevalue input - However I would like d variable to only be applied to the rows that contain [type] == 'aa'

#apply e variable to rows that contain [type] = 'bb'

d = {
    'mig1': pd.DateOffset(months=5),
    'de': pd.DateOffset(months=3),
    're': pd.DateOffset(months=1),
}

e = {
        'mig1': pd.DateOffset(months=5),
        
    }


s = pd.Series(d).rsub(datevalue)
df.assign(**{**s, 'mig2': datevalue})

Any suggestion is appreciated

1
  • Sounds a lot like this question. Commented Dec 29, 2021 at 0:09

2 Answers 2

1

Try:

datevalue = pd.to_datetime(input("Enter shift: "), dayfirst=False)  # 08/01/2022

new_values = pd.Series(d).rsub(datevalue) \
                         .append(pd.Series({'mig2': datevalue})) \
                         .dt.strftime('%-m/%-d/%Y')
df.update(pd.DataFrame([new_values], index=df[df['type'] == 'aa'].index))

# do the same for type='bb' and 'e' dict
new_values = pd.Series(e).rsub(datevalue) \
                         .append(pd.Series({'mig2': datevalue})) \
                         .dt.strftime('%-m/%-d/%Y')
df.update(pd.DataFrame([new_values], index=df[df['type'] == 'bb'].index))

Output:

>>> df
  location type      mig1         de      mig2        re
0       ny   aa  3/1/2022   5/1/2022  8/1/2022  7/1/2022
1       ny   aa  3/1/2022   5/1/2022  8/1/2022  7/1/2022
2       ca   aa  3/1/2022   5/1/2022  8/1/2022  7/1/2022
3       tx   bb  3/1/2022  11/1/2021  8/1/2022  3/1/2022
Sign up to request clarification or add additional context in comments.

15 Comments

Ok will try- how did you get the final output? Did you concatenate the ‘d’ and ‘e’ output?
I understood now mig2 is datevalue.
Did you have time to try my answer? I also answered to your (similar) question
Will try- I marked other soln correct as I see how the output will be derived thank you. I like your explanation as well
It's to avoid horizontal scrollbar. You can remove them to reformat the code on one line.
|
1
#Coerce dates to datetime
df1=df1.set_index(['location','type']).apply(lambda x: pd.to_datetime(x,format='%d/%m/%Y'))

#Set non dates as index, slice level two and impose the datetifference
df1.loc[ ( slice(None), 'aa' ),  : ]=df1.loc[ ( slice(None), 'aa' ),  : ]-pd.to_timedelta(5, unit='d')


                  mig1         de       mig2         re
location type                                            
ny       aa   2021-01-03 2021-01-05 2021-12-27 2021-12-28
         aa   2021-01-03 2021-01-05 2021-12-27 2021-12-28
ca       aa   2021-01-03 2021-01-05 2021-12-27 2021-12-28
tx       bb   2021-01-09 2021-01-11 2022-01-02 2022-01-03

7 Comments

hi- was wondering if input statement is used? - needing to input a specific date
Not sure I understood your question. Input what date and how?
Ok. I am using input() function to input the date : 8/01/2022- whenever a specific date is entered the dates will shift. Only wish to apply to certain rows though
possible but quite a complicated model especially with the multiple date fields. Will try figure it out
As I figure that out, will leave my answer just in case someone needs to use slice :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.