Python 2.7: Shift A Dataframe By Day And A Column Value
I have a dataframe named df1 as following: df1: a b id 2010-01-01 2 3 21 2010-01-01 2 4 22 2010-01-01 3 5 23 2010-01-01 4 6
Solution 1:
If all groups are same length (in sample 4) and DatetimeIndex
is sorted:
df2=df.shift((df.index==df.index[0]).sum())print(df2)abid2010-01-01 NaNNaNNaN2010-01-01 NaNNaNNaN2010-01-01 NaNNaNNaN2010-01-01 NaNNaNNaN2010-01-02 2.03.021.02010-01-02 2.04.022.02010-01-02 3.05.023.02010-01-02 4.06.024.02010-01-03 1.04.021.02010-01-03 2.05.022.02010-01-03 3.06.023.02010-01-03 4.07.024.0
But if need shift values of index by one day:
df3=df.shift(1,freq='D')print(df3)abid2010-01-02 23212010-01-02 24222010-01-02 35232010-01-02 46242010-01-03 14212010-01-03 25222010-01-03 36232010-01-03 47242010-01-04 18212010-01-04 29222010-01-04 310232010-01-04 41124
Solution 2:
One of the way is with the help of shape if the dates are sorted i.e
df.shift(df.loc[df.index[0]].shape[0])
# Or len
df.shift(len(df.loc[df.index[0]]))
Output :
a b id 2010-01-01 NaN NaN NaN 2010-01-01 NaN NaN NaN 2010-01-01 NaN NaN NaN 2010-01-01 NaN NaN NaN 2010-01-02 2.0 3.0 21.0 2010-01-02 2.0 4.0 22.0 2010-01-02 3.0 5.0 23.0 2010-01-02 4.0 6.0 24.0 2010-01-03 1.0 4.0 21.0 2010-01-03 2.0 5.0 22.0 2010-01-03 3.0 6.0 23.0 2010-01-03 4.0 7.0 24.0
Post a Comment for "Python 2.7: Shift A Dataframe By Day And A Column Value"