在Pandas数据框中,如何在第二列的条件下提取同一列中不同行的值之间的差异? [英] In a Pandas dataframe, how can I extract the difference between the values on separate rows within the same column, conditional on a second column?

查看:117
本文介绍了在Pandas数据框中,如何在第二列的条件下提取同一列中不同行的值之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个较大项目的一部分,但是我将我的问题分解为多个步骤,因此这是第一步.像这样获取Pandas数据框:

This is part of a larger project, but I've broken my problem down into steps, so here's the first step. Take a Pandas dataframe, like this:

index | user   time     
---------------------
 0      F       0   
 1      T       0   
 2      T       0   
 3      T       1   
 4      B       1 
 5      K       2 
 6      J       2 
 7      T       3 
 8      J       4 
 9      B       4 

对于每个唯一用户,是否可以在某些条件下提取时间"列中的值之间的差异?

For each unique user, can I extract the difference between the values in column "time," but with some conditions?

例如,有一个用户J的两个实例,并且这两个实例之间的时间"差为2.我可以提取这两行之间的差2吗?然后,如果该用户再次出现,请提取该行与该用户在数据框中的先前外观之间的差异?

So, for example, there are two instances of user J, and the "time" difference between these two instances is 2. Can I extract the difference, 2, between these two rows? Then if that user appears again, extract the difference between that row and the previous appearance of that user in the dataframe?

推荐答案

我认为需要

I believe need DataFrameGroupBy.diff:

df['new'] = df.groupby('user')['time'].diff()
print (df)
  user  time  new
0    F     0  NaN
1    T     0  NaN
2    T     0  0.0
3    T     1  1.0
4    B     1  NaN
5    K     2  NaN
6    J     2  NaN
7    T     3  2.0
8    J     4  2.0
9    B     4  3.0

这篇关于在Pandas数据框中,如何在第二列的条件下提取同一列中不同行的值之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆