Home > Software design >  How do I calculate colums in pandas dataframe?
How do I calculate colums in pandas dataframe?

Time:11-30

I basically want to type a formula and apply it on the rest below. My problem is that I don't know how to tell pandas that it has to pick two items out of the same column and for every new calculation, it has to "move down" the cell selection.

CodePudding user response:

You can do this:

For

  a
0  1.0
1  3.0
2  5.0
3  2.0

then

df['d'] =(df['a'] df['a'].shift(-1))*df['a'].shift(-1)

gives

 a     d
0  1.0  12.0
1  3.0  40.0
2  5.0  14.0
3  2.0   NaN

CodePudding user response:

Using shift function will give you get the result you are looking for:

# Assuming your dataframe it's called df and the columns are 'A' and 'B'
df["B"] = (df["A"]   df["A"].shift(-1))*df["A"].shift(-1)

Panda's shift function moves every row (up or down) the number of positions considering the number given as a parameter.

If you move every row -1 position, you'll get a dataframe which has every row moved one position up (making the first row to disappear and the last one filled with NaN).

To get (A1 A2)*(A2), you'll need to point at every cell below being A1 the actual cell df["A"] and A2 the cell below (df["A"].shift(-1))

  • Related