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

example:

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))

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])
```

### Answer：

To obtain the lagged difference of a series use

`df['my_column'].diff()`

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

See the documentation for more info

**EDIT**

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).

### Answer：

This is how I solved my problem for now using numpy

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

```
s1=np.array(df[0])
s=s1[1:]-s1[0:-1]
s[s<0]=s[s<0]+(2**32)
```

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?

Tags: excel, pandas, pythonpython