Home » excel » python – Is there an equivalent of an excel formula in pandas

python – Is there an equivalent of an excel formula in pandas

Posted by: admin May 14, 2020 Leave a comment


I have a time-series dataset and I need to find the difference in my field for
every tick.

In excel, I can accomplish this easily by writing a formula B2=A2-A1 and copying this formula down the entire B column


enter image description here

Is there a straightforward way to do this with pandas?

I have considered apply() but looks like it can only work on 1 element of a series

My other option will be to create an Numpy array out of my column A and run a loop through it. However, that seems like a roundabout way and gets even more complicated if I need to refer elements from different

The actual Excel formula that I need to translate is =IF((A3-A2)>0, (A3-A2), A3+(4294967296-A2))

How to&Answers:

This may help. I haven’t tested: for this, please provide some data (in text form) with desired output.

df[1] = np.where(df[0].diff() > 0, df[0].diff(), df[0].shift() + 2**32 - df[0])


To obtain the lagged difference of a series use


You can specify the lag as well, if you wanted to do something different than 1.

See the documentation for more info

Here is a possible way to address your formula:

df = pd.DataFrame({'A': [20, 22, 25, 43, 23, 45, 67, 50, 70]})
df['result'] = df['A'].diff()
df['result'][ df['result'] < 0 ] += 2**32  # you can disregard the pandas warning

Based on your logic you can simply take the diff and if the result is less than zero then add the constant 2**32. And it should be a little more efficient than taking the diff a few times (although probably this is not a big concern).


This is how I solved my problem for now using numpy

(translating Excel formula =IF((A3-A2)>0, (A3-A2), A3+(2**32-A2)))


I do feel there can be a more elegant solution for this natively in pandas. May be if df.diff() could take a lambda parameter?