简体   繁体   English

在pandas数据框中创建一列,该列计算两行之间的差异

[英]create a column in a pandas dataframe which calculates the difference between two rows

I want to create a new column called df[column_name] , where the result is the difference between the current row and the row above it. 我想创建一个名为df[column_name]的新列,其中的结果是当前行与其上方的行之间的差异。

In the case of the first row there is nothing above it so change is 0. for the second row change is 2 (26-24) and so on. 对于第一行,上面没有任何内容,因此更改为0。对于第二行,更改为2(26-24),依此类推。

dates | data | result
24-09    24      0
25-09    26      2
26-09    27      1
27-09    28      1
28-09    26     -2

You can use the diff method for this, together with fillna to fill the first NaN with a 0: 您可以为此使用diff方法,并与fillna一起使用fillna填充第一个NaN:

df['data'].diff().fillna(0)

Example: 例:

In [6]: df = pd.DataFrame({'data':[24,26,27,28,26]})

In [7]: df['result'] = df['data'].diff().fillna(0)

In [8]: df
Out[8]: 
   data  result
0    24       0
1    26       2
2    27       1
3    28       1
4    26      -2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM