Search code examples
pythonpandasdataframecalculated-columns

Calculating values using begining value in column Pandas (python)


Good time a day. I have a table with data, which you can download by that link:

https://docs.google.com/spreadsheets/d/1EOEuBRYEpDNz5p1m-VD5fkJS7-DmmMlwIgmibrYRWZM/edit?usp=sharing

I should calculate column "values" by defining beginner value (in table the beginner value is 1) , using formula: B2 multiple by A3, B3 multiple by A4 ... in column "values".

How i can do it in Pandas? I would bless if you can help me to do it. enter image description here


Solution

  • You can use the cumprod (cumulative product) method on the Index values, after replacing the first value by 1:

    import pandas as pd
    
    df = pd.DataFrame({'Index': [1.066373, 1.126100, 1.081077, 1.048902, 1.068631]})
    df['Values'] = df.Index
    df.Values[0] = 1
    df.Values = df.Values.cumprod()
    df
    
        Index       Values
    0   1.066373    1.000000
    1   1.126100    1.126100
    2   1.081077    1.217401
    3   1.048902    1.276934
    4   1.068631    1.364571