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

点点圈 提交于 2019-11-27 00:39:57

Use shift.

df['dA'] = df['A'] - df['A'].shift(-1)

You could use diff and pass -1 as the periods argument:

>>> df = pd.DataFrame({"A": [9, 4, 2, 1], "B": [12, 7, 5, 4]})
>>> df["dA"] = df["A"].diff(-1)
>>> df
   A   B  dA
0  9  12   5
1  4   7   2
2  2   5   1
3  1   4 NaN

[4 rows x 3 columns]
Seth Okeyo

When using data in CSV, this would work perfectly:

my_data = pd.read_csv('sale_data.csv')
df = pd.DataFrame(my_data)
df['New_column'] = df['target_column'].diff(1)
print(df) #for the console but not necessary 

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!