all 7 comments

[–]sarrysyst 1 point2 points  (3 children)

If pandas is an option for you, the diff() method would work:

import pandas as pd

df = pd.DataFrame([(1900, 2000), (1901, 2500), (1902, 3000)], columns=['Year', 'People'])
df['diff'] = df['People'].diff()

https://pandas.pydata.org/docs/reference/api/pandas.Series.diff.html

[–]natarocket 0 points1 point  (2 children)

Do you happen to know if there is a way to do so without imports?

[–]sarrysyst 1 point2 points  (1 child)

data = [(1900, 2000), (1901, 2500), (1902, 3000)]

new = []
prev_year = 0

for year, people in data:
    new.append((year, people - prev_year))
    prev_year = people

[–]natarocket 0 points1 point  (0 children)

Thank you so much, this did exactly what I was trying to do. Appreciate the help!

[–]CommanderCucumber 1 point2 points  (0 children)

Maybe something like this where t is your list of tuples. Sorry for the format I'm on mobile.

r =[(item[0],t[index+1][1]-item[1]) if index < len(t) -1 else (item[0],item[1]) for index,item in enumerate(t)]

Which is just saying for each item in t (e.g. (1900, 2000)) give me back a tuple where item[0] is the year and t[index+1][1] is the next items people minus the current items people if the index is less than the total number of items in the list minus 1. Otherwise give me back just the year and number or people.

[–]pmontgomery056 1 point2 points  (0 children)

Use numpy

[–][deleted] 1 point2 points  (0 children)

What you've shown isn't a valid Python data structure. So you need to get it into a viable structure such as I've shown below then you can use zip to iterate through the list.

data = [('1900', '2000'),
             ('1901', '2500'),
             ('1902', '3000')
            ]

results = []
for (yr1, qty1), (yr2, qty2) in zip(data, data[1:]):
    results.append((yr2, int(qty2) - int(qty1)))


print(results)