Home > Enterprise >  Python pandas add N multiplied by the value of the current second and add with the value of N multip
Python pandas add N multiplied by the value of the current second and add with the value of N multip

Time:08-19

I have a csv file in which I would like to make some calculation.

My csv looks something like this:

Time Value1 Value2
10:00:00 4 1
10:00:01 5 0
10:00:02 4 4
10:00:03 5 3
10:00:04 4 1
10:00:05 5 2
10:00:06 4 4
10:00:07 5 8
10:00:08 4 4
10:00:09 5 8

In the background, I add the variable N = 10

And I would like to add a new column to the same csv file with a calculation using the formula

Something like N 1 * Value 1 of its own line N 1 * Value 2 of the previous line

To get something like this:

Time Value1 Value2 Value3
10:00:00 4 1 44
10:00:01 5 0 66
10:00:02 4 4 44
10:00:03 5 3 99
10:00:04 4 1 77
10:00:05 5 2 66
10:00:06 4 4 66
10:00:07 5 8 99
10:00:08 4 4 132
10:00:09 5 8 99

I tried something like

out = df.groupby(['Time']) out = %N 1 * df['Value1'] %N 1 * df['Value2']

But it doesn't quite work

CodePudding user response:

import pandas as pd

df = pd.DataFrame(
    [['10:00:00', 4, 1], ['10:00:01', 5, 0]],
    columns=['Time', 'Value1', 'Value2']
)

N = 10
df['Value3'] = (N   1) * (df.Value1   df.Value2.shift(1))
print(df)

prints

index Time Value1 Value2 Value3
0 10:00:00 4 1 NaN
1 10:00:01 5 0 66.0
  • Related