简体   繁体   English

Pandas DataFrame按两列分组,并获得第一和最后

[英]Pandas DataFrame groupby two columns and get first and last

I have a DataFrame Like following. 我有一个像下面的DataFrame

df = pd.DataFrame({'id' : [1,1,2,3,2],
                'value'  : ["a","b","a","a","c"], 'Time' : ['6/Nov/2012 23:59:59 -0600','6/Nov/2012 00:00:05 -0600','7/Nov/2012 00:00:09 -0600','27/Nov/2012 00:00:13 -0600','27/Nov/2012 00:00:17 -0600']})

I need to get an output like following. 我需要获得如下输出。

combined_id | enter time | exit time | time difference

combined_id should be created by grouping 'id' and 'value' 应通过将“ id”和“ value”分组来创建Combined_id

g = df.groupby(['id', 'value'])

Following doesn't work with grouping by two columns. 以下操作不适用于按两列分组。 (How to use first() and last() here as enter and exit times?) (如何在此处使用first()last()作为进入和退出时间?)

df['enter'] = g.apply(lambda x: x.first())

To get difference would following work? 获得差异会跟随工作吗?

df['delta'] = (df['exit']-df['enter'].shift()).fillna(0)

First ensure you're column is a proper datetime column: 首先确保您的列是正确的日期时间列:

In [11]: df['Time'] = pd.to_datetime(df['Time'])

Now, you can do the groupby and use agg with the first and last groupby methods: 现在,您可以执行groupby并将agg与第firstlast groupby方法一起使用:

In [12]: g = df.groupby(['id', 'value'])

In [13]: res = g['Time'].agg({'first': 'first', 'last': 'last'})

In [14]: res = g['Time'].agg({'enter': 'first', 'exit': 'last'})

In [15]: res['time_diff'] = res['exit'] - res['enter']

In [16]: res
Out[16]:
                        exit               enter  time_diff
id value
1  a     2012-11-06 23:59:59 2012-11-06 23:59:59     0 days
   b     2012-11-06 00:00:05 2012-11-06 00:00:05     0 days
2  a     2012-11-07 00:00:09 2012-11-07 00:00:09     0 days
   c     2012-11-27 00:00:17 2012-11-27 00:00:17     0 days
3  a     2012-11-27 00:00:13 2012-11-27 00:00:13     0 days

Note: this is a bit of a boring example since there is only one item in each group... 注意:这是一个无聊的示例,因为每个组中只有一个项目...

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

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