0

The pandas data frame column of interest is as follows:

data['AREA_ring']
Out[85]: 
0    1.114714e+06
1    1.003243e+07
2    2.786786e+07
3    5.462100e+07
4    9.029185e+07
5    1.348804e+08
Name: AREA_ring, dtype: float64

I'm trying to subtract the previous value for each value in the column except for the first value, which I would like to stay the same. ie. 1.003243e+07 - 1.114714e+06 and so on...

What I've tried is the following:

data['AREA_ring'] = [x - y for x, y in zip(data['AREA_ring'][1:6], data['AREA_ring'] 
[0:5])]

I am returned the following:

ValueError: Length of values (5) does not match length of index (6)

This doesn't make sense to me because when I type out the content for data['AREA_ring'][1:6], data['AREA_ring'][0:5] I am returned the same amount of values:

data['AREA_ring'][1:6]
Out[80]: 
1    1.003243e+07
2    2.786786e+07
3    5.462100e+07
4    9.029185e+07
5    1.348804e+08

data['AREA_ring'][0:5]
Out[65]: 
0    1.114714e+06
1    1.003243e+07
2    2.786786e+07
3    5.462100e+07
4    9.029185e+07

As you can see, there are 5 elements in each so I'm not sure what the issue is.

I'd like some assistance troubleshooting this.

Astroturf
  • 101
  • 2
    Use `diff`: `data['AREA_ring'].diff()`. Your solution didn't work because pandas aligns the indexes before operations – mozway Mar 09 '22 at 03:22

0 Answers0