Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mathematical calculation using for loop in a dataframe

I have a dataframe which looks like this

df= 

    Time                  x          y
0   2018-09-13 01:17:00  5.0        0.0
1   2018-09-13 02:17:00  9.0        0.0
2   2018-09-13 03:17:00  2.0        1.0
3   2018-09-13 04:17:00  1.0        0.0

…….
I want to iterate through this whole dataframe and calculate a new variable z.
The value of z would be z= z[prev]+ x-y

for example, the final output would be

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Time                         z
0   2018-09-13 01:17:00      5    #[0+5-0]
1   2018-09-13 02:17:00      14   #[5+9-0]
2   2018-09-13 03:17:00      15   #[14+2-1]
3   2018-09-13 04:17:00      16   #[15+1-0]

…..

I am finding it difficult to iterate over the time series data.

I have tried the following but it is not working.

for i,row in df.iterrows():
    z=0
    row['z']=row['z']+row['x']-row['y']

print[z]

>Solution :

In your case do cumsum

df['new'] = df.x.sub(df.y).cumsum()
Out[410]: 
0  2018-09-13     5.0
1  2018-09-13    14.0
2  2018-09-13    15.0
3  2018-09-13    16.0
dtype: float64
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading