Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13951

Difference across rows in Pandas DataFrame

$
0
0

How do I create df2 directly using df as input? I can't wrap my head around it...

>>> import pandas as pd>>> data = {'Year': [1990, 1990, 1990, 2022, 2022, 2022], 'City': ['Paris', 'Berlin', 'Lisbon', 'Paris', 'Berlin', 'Lisbon'], 'Population': [2.15, 3.40, 2.54, 2.11, 3.57, 2.99]}>>> df = pd.DataFrame.from_dict(data)   Year    City  Population0  1990   Paris        2.151  1990  Berlin        3.402  1990  Lisbon        2.543  2022   Paris        2.114  2022  Berlin        3.575  2022  Lisbon        2.99>>> data2 = {'City': ['Paris', 'Berlin', 'Lisbon'], 'Population change': [2.11 - 2.15, 3.57 - 3.40, 2.99 - 2.54]}>>> df2 = pd.DataFrame.from_dict(data2)     City  Population change0   Paris              -0.041  Berlin               0.172  Lisbon               0.45

Viewing all articles
Browse latest Browse all 13951

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>