Home > database >  Python pandas Addition and subtraction by sequence
Python pandas Addition and subtraction by sequence

Time:08-31

I have a two column frame that looks like this:

value,Sequence
12506,-1
12501,-2
12513,-3
12513,1
12521,2
12501,3
12583,-1
12594,-2
12598,1
12589,-1
12615,1
12615,2
12611,3
12573,-1
12593,-2
12564,-3

I want to get a new data frame with calculations valuet the combined label

How do I want to add in sequence:

1)(Value = 12501) by label -2,

(Value = 12513) by label -3

label(-3) - label(-2)

12501 - 12513 = -12

2)(Value = 12521) by label 2,

(Value = 12501) by label 3

label(2) - label(3)

12521 - 12501 = 20

If after the labels (-2, 2) there is no label (-3,3) and there is a label (-1, 1), then the calculations will be as follows

3)(Value = 12594) by label -2,

(Value = 12598) by label 1

label(-2) - label(1)

12594 - 12598 = -4

If after the labels (-1,1) there are (1, -1), then calculations do not occur

I want to get a data frame like this:

Calculation
-12
20
-4
4
-29

It is possible to do this using Pandas?

CodePudding user response:

Yeah I just did it but I just don't really know why and how I managed to understand what you want.(edit: I think not but pretty close)

import pandas as pd
import numpy as np

d = {'value':[12506,12501,12513,12513,
              12521,12501,12583,12594,
              12598,12589,12615,12615,
              12611,12573,12593,12564], 
     'Sequence':[-1,-2,-3,1,2,3,-1,-2,1,-1,1,2,3,-1,-2,-3]}
df = pd.DataFrame(data=d)

df['value_shifted'] = df["value"].shift(-1)
df['Sequence_shifted'] = df["Sequence"].shift(-1)
new_df = df.where(np.abs(df['Sequence']) == 2).dropna()
new_df['calculation'] = new_df['value'] - new_df['value_shifted']

Result(new_df):

      value  Sequence  value_shifted  Sequence_shifted  calculation
1   12501.0      -2.0        12513.0              -3.0        -12.0
4   12521.0       2.0        12501.0               3.0         20.0
7   12594.0      -2.0        12598.0               1.0         -4.0
11  12615.0       2.0        12611.0               3.0          4.0
14  12593.0      -2.0        12564.0              -3.0         29.0
  • Related