Skip to content Skip to sidebar Skip to footer

How To Use Shift() Within Apply() In Dataframe And Still Access Full Series?

I have a dataframe where I am trying to create a new column based on applying a lambda to two columns. closeunadj qtr_timedelta date 2021-05-18 128.75 107 2021-05-19 13

Solution 1:

One way is to use the full series final_merge['closeunadj'] for the shift in the apply, then use loc with x.name (that is the index of the current row) to get the right value. Not sure it is the most efficient but because your dataframe is about 1K rows, should be fine

final_merge['qtr_gwth'] = (
    final_merge[['closeunadj', 'qtr_timedelta']]
      .apply(lambda x : x['closeunadj'] / final_merge['closeunadj'].shift(x['qtr_timedelta']).loc[x.name] - 1, 
             axis=1)
)

Post a Comment for "How To Use Shift() Within Apply() In Dataframe And Still Access Full Series?"