Adding a column thats result of difference in consecutive rows in pandas

后端 未结 4 1311
旧巷少年郎
旧巷少年郎 2020-11-28 23:07

Lets say I have a dataframe like this

    A   B
0   a   b
1   c   d
2   e   f 
3   g   h

0,1,2,3 are times, a, c, e, g is one time series a

4条回答
  •  鱼传尺愫
    2020-11-28 23:49

    Rolling differences can also be calculated this way:

    df=pd.DataFrame(my_data)
    my_data = pd.read_csv('sales_data.csv')
    i=0
    j=1
    while j < len(df['Target_column']):
        j=df['Target_column'][i+1] - df['Target_column'][i] #the difference btwn two values in a column.
        i+=1 #move to the next value in the column.
        j+=1 #next value in the new column.
        print(j)
    

提交回复
热议问题