I am using the shift method for a data series in pandas (documentation).
Is it possible do a cyclic shift, i.e. the first value become the last value, in one step?
>>> input
Out[20]:
5 0.995232
15 0.999794
25 1.006853
35 0.997781
45 0.981553
Name: vRatio, dtype: float64
>>> input.shift()
Out[21]:
5 NaN
15 0.995232
25 0.999794
35 1.006853
45 0.997781
Name: vRatio, dtype: float64
desired output:
Out[21]:
5 0.981553
15 0.995232
25 0.999794
35 1.006853
45 0.997781
Name: vRatio, dtype: float64
shift() function to shift the data of the given Series object by -2 periods. Output : Now we will use Series. shift() function to shift the data in the given series object by -2 periods.
shift() function Shift index by desired number of periods with an optional time freq. This function takes a scalar parameter called the period, which represents the number of shifts to be made over the desired axis. This function is very helpful when dealing with time-series data.
shift() If you want to shift your column or subtract the column value with the previous row value from the DataFrame, you can do it by using the shift() function. It consists of a scalar parameter called period, which is responsible for showing the number of shifts to be made over the desired axis.
iloc attribute enables purely integer-location based indexing for selection by position over the given Series object. Example #1: Use Series. iloc attribute to perform indexing over the given Series object.
Pandas Series.shift () function shift index by desired number of periods with an optional time freq. When freq is not passed, shift the index without realigning the data. Syntax: Series.shift (periods=1, freq=None, axis=0, fill_value=None) Parameter : periods : Number of periods to shift. Can be positive or negative.
Pandas Series.shift () function shift index by desired number of periods with an optional time freq. When freq is not passed, shift the index without realigning the data.
periods : Number of periods to shift. Can be positive or negative. axis : Shift direction. Returns : Copy of input object, shifted. Example #1: Use Series.shift () function to shift the data of the given Series object by 2 periods. Now we will use Series.shift () function to shift the data in the given series object by 2 periods.
frequency represents the multiple parameters such as data offset, timedelta, time series modules and time rule string. axis represents 0 to represent rows and 1 to represent columns. How Pandas shift () Function works?
You can use np.roll
to cycle the index values and pass this as the values to reindex
:
In [23]:
df.reindex(index=np.roll(df.index,1))
Out[23]:
vRatio
index
45 0.981553
5 0.995232
15 0.999794
25 1.006853
35 0.997781
If you want to preserve your index then you can just overwrite the values again using np.roll
:
In [25]:
df['vRatio'] = np.roll(df['vRatio'],1)
df
Out[25]:
vRatio
index
5 0.981553
15 0.995232
25 0.999794
35 1.006853
45 0.997781
Here's a slight modification of @EdChum 's great answer, which I find more useful in situations where I want to avoid an assignment:
pandas.DataFrame(np.roll(df.values, 1), index=df.index)
or for Series:
pandas.Series(np.roll(ser.values, 1), index=ser.index)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With