# How to refer to previous row with itertuples()

**URL:** <https://discuss.python.org/t/how-to-refer-to-previous-row-with-itertuples/104497>\
**Category:** Python Help\
**Tags:** help\
**Created:** [October 22, 2025, 1:50am UTC](https://discuss.python.org/t/how-to-refer-to-previous-row-with-itertuples/104497 "2025-10-22T01:50:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Python1231](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Python1231](https://discuss.python.org/u/Python1231)\
**Post date:** [October 22, 2025, 1:50am UTC](https://discuss.python.org/t/how-to-refer-to-previous-row-with-itertuples/104497/1 "2025-10-22T01:50:05Z")

</div>

I’ve imported a CSV file using pandas. It has 2 columns: a date and a payment amount. I’d like to iterate through it and calculate the elapse time between rows. But when I use `for row in pymtdata.itertuples():` I can’t figure out how to access values in the previous row. Any suggestions?

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [October 22, 2025, 1:58am UTC](https://discuss.python.org/t/how-to-refer-to-previous-row-with-itertuples/104497/2 "2025-10-22T01:58:19Z")

</div>

The first element of the tuple is the index. If you can figure out the previous row from that value (i.e. if they’re sequential) you can retrieve said row that way.

Alternatively, you could wrap your iterator with `enumerate` and then you’d have a sequential index alongside your tuple.

As a final option: pandas can compute this kind of thing. If it has parsed your dates properly, it should be possible to compute the row-to-row difference with the [`diff`](https://pandas.pydata.org/docs/reference/api/pandas.Series.diff.html) method.

---

<div class="post-metadata">

**Author:** ![BrenBarn](https://avatars.discourse-cdn.com/v4/letter/b/74df32/32.png) [@BrenBarn](https://discuss.python.org/u/BrenBarn)\
**Post date:** [October 22, 2025, 2:24am UTC](https://discuss.python.org/t/how-to-refer-to-previous-row-with-itertuples/104497/3 "2025-10-22T02:24:51Z")

</div>

As @jamestwebber noted, the `.diff` method will already compute a difference for you. For slightly more general needs, there is also a `.shift` method on Series that you can use to create a shifted version of a column, where the value in the shifted column at row `n` is the value in original column at row `n-1` (or `n+1`, or actually you can shift by any offset) . This can be used to create a new column that is a shifted version of an existing column, effectively allowing you to use ordinary operations on a single row that take account of values in the next/previous row.

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [October 3, 2026, 1:43am UTC](https://discuss.python.org/t/how-to-refer-to-previous-row-with-itertuples/104497/4 "2026-10-03T01:43:53Z")

</div>

The generic solution, not tied to Pandas, is to set `prev = None` before looping and update ‘prev’ at the end of each loop. Being able to add shifted columns is really nice.
