Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 800
Scarlet Avatar asked Aug 27 '15 18:08

Scarlet


People also ask

Can I use Pandas instead of NumPy?

When we have to work on Tabular data, we prefer the pandas module. When we have to work on Numerical data, we prefer the numpy module. The powerful tools of pandas are Data frame and Series. Whereas the powerful tool of numpy is Arrays.

What is rolling function in Pandas?

Pandas Series: rolling() function The rolling() function is used to provide rolling window calculations. Syntax: Series.rolling(self, window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None) Parameters: Name. Description.

What is NP roll in Python?

The numpy. roll() function rolls array elements along the specified axis. Basically what happens is that elements of the input array are being shifted. If an element is being rolled first to the last position, it is rolled back to the first position.


1 Answers

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)
like image 89
CT Zhu Avatar answered Sep 23 '22 03:09

CT Zhu