简体   繁体   中英

pandas equivalent to numpy.roll

I have a pandas dataframe and I'd like to add a new column that has the contents of an existing column, but shifted relative to the rest of the data frame. I'd also like the value that drops off the bottom to get rolled around to the top.

For example if this is my dataframe:

>>> myDF
   coord  coverage
0      1         1
1      2        10
2      3        50

I want to get this:

>>> myDF_shifted
   coord  coverage  coverage_shifted
0      1         1                50
1      2        10                 1
2      3        50                10

(This is just a simplified example - in real life, my dataframes are larger and I will need to shift by more than one unit)

This is what I've tried and what I get back:

>>> myDF['coverage_shifted'] = myDF.coverage.shift(1)
>>> myDF
   coord  coverage  coverage_shifted
0      1         1               NaN
1      2        10                 1
2      3        50                10

So I can create the shifted column, but I don't know how to roll the bottom value around to the top. From internet searches I think that numpy lets you do this with "numpy.roll". Is there a pandas equivalent?

Pandas probably doesn't provide an off-the-shelf method to do the exactly what you described, however if you can move a little but out of the box, numpy has exactly that

In your case it is:

import numpy as np
myDF['coverage_shifted'] = np.roll(df.coverage, 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM