Cyclic shift of a pandas series

前端 未结 3 1499
我在风中等你
我在风中等你 2021-02-19 02:15

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?

3条回答
  •  滥情空心
    2021-02-19 02:55

    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)
    

提交回复
热议问题