-3

I'm doing a research on land consumption and demographic growth. I have a dataframe with a chronological sequence of population listed for a period of years.

import pandas as pd
df = pd.DataFrame({'year': [2014, 2015, 2016, 2017, 2018], 'population': [66354, 63322,83381, 91563, 93709]})

In other vain I had the number of buildings constructed grouped by year.

df2 = pd.DataFrame({'year': [2014, 2015, 2016, 2017, 2018], 'numberOfBu': [1348, 900, 1137, 865, 235]})

To make a comparison with the sequence between buildings and population I should make an incremental adition year by year for the buildings -as it's done with the values of population. So I must add the first value with the second, the result of both with the third and so on.

Is there a solution with pandas?

Rodrigo Vargas
  • 273
  • 3
  • 17
  • 2
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on how to ask a good question may also be useful – yatu Jun 02 '19 at 18:25
  • Hi @yatu thanks for your recommendations. I've tried to make a it a bit more readable. Hope it can help. – Rodrigo Vargas Jun 02 '19 at 19:12

1 Answers1

0

IIUC you're searching for the cumulative sum function cumsum():

df.population.cumsum()
# 0     66354
# 1    129676                                               
# 2    213057                                           
# 3    304620                                               
# 4    398329                                                
# Name: population, dtype: int64    
SpghttCd
  • 10,510
  • 2
  • 20
  • 25