简体   繁体   English

使用分区计算Pandas中条目之间的增量

[英]Calculate the delta between entries in Pandas using partitions

I'm using Dataframe in Pandas , and I would like to calculate the delta between each adjacent rows, using a partition. 我在Pandas使用Dataframe ,我想使用分区计算每个相邻行之间的差值。

For example, this is my initial set after sorting it by A and B: 例如,这是我用A和B排序之后的初始设置:

    A   B    
1   12  40
2   12  50
3   12  65
4   23  30
5   23  45
6   23  60

I want to calculate the delta between adjacent B values, partitioned by A. If we define C as result, the final table should look like this: 我想计算相邻B值之间的增量,用A分区。如果我们将C定义为结果,则最终表应该如下所示:

    A   B   C   
1   12  40  NaN
2   12  50  10
3   12  65  15
4   23  30  NaN
5   23  45  15
6   23  75  30

The reason for the NaN is that we cannot calculate delta for the minimum number in each partition. NaN的原因是我们无法计算每个分区中最小数量的增量。

You can group by column A and take the difference: 您可以按A列进行分组并获取差异:

df['C'] = df.groupby('A')['B'].diff()

df
Out: 
    A   B     C
1  12  40   NaN
2  12  50  10.0
3  12  65  15.0
4  23  30   NaN
5  23  45  15.0
6  23  60  15.0

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

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