简体   繁体   English

如何在熊猫中获取第一次和最后一次出现的项目

[英]How to obtain first and last occurrence of an item in pandas

I'm analysing data that comes from different sensors.我正在分析来自不同传感器的数据。 The sensors become active (1) when used.传感器在使用时变为活动状态 (1)。 However, I only need the time (and date) from the first and last activation, but not any from in between.但是,我只需要第一次和最后一次激活的时间(和日期),而不需要两者之间的时间(和日期)。 When found, I need create a new DataFrame with the Time and Date of the first and last occurrence, along with 'User' and 'Activity'.找到后,我需要创建一个新的 DataFrame,其中包含第一次和最后一次出现的时间和日期,以及“用户”和“活动”。

I've tried to iterate through each row and build a series of if-then statements, but with no luck.我试图遍历每一行并构建一系列 if-then 语句,但没有运气。 I was wondering if there's a pandas function that would allow me to do it efficiently?我想知道是否有一个熊猫功能可以让我有效地做到这一点? Here's a subset of my data.这是我的数据的一个子集。

I'm just starting to get the hang of pandas, so any help will be much appreciated.我刚刚开始掌握熊猫的窍门,所以任何帮助都将不胜感激。

cheers!干杯!

import pandas as pd            
cols=['User', 'Activity', 'Coaster1', 'Coaster2', 'Coaster3',
           'Coaster4', 'Coaster5', 'Coffee', 'Door', 'Fridge', u'coldWater',
           'hotWater', 'SensorDate', 'SensorTime', 'RegisteredTime']

 data=[['Chris', 'coffee + hot water', 0, 0.0, 0.0, 0, 0, 0.0, 1.0, 0.0,
            0.0, 0.0, '2015-09-21', '13:05:54', '13:09:00'],
           ['Chris', 'coffee + hot water', 0, 0.0, 0.0, 0, 0, 0.0, 1.0, 0.0,
            0.0, 0.0, '2015-09-21', '13:05:54', '13:09:00'],
           ['Chris', 'coffee + hot water', 0, 0.0, 0.0, 0, 0, 0.0, 1.0, 0.0,
            0.0, 0.0, '2015-09-21', '13:05:55', '13:09:00'],
           ['Chris', 'coffee + hot water', 0, 0.0, 0.0, 0, 0, 0.0, 1.0, 0.0,
            0.0, 0.0, '2015-09-21', '13:05:55', '13:09:00'],
           ['Chris', 'coffee + hot water', 0, 0.0, 0.0, 0, 0, 0.0, 1.0, 0.0,
            0.0, 0.0, '2015-09-21', '13:05:56', '13:09:00'],
           ['Chris', 'coffee + hot water', 0, 0.0, 0.0, 0, 0, 0.0, 1.0, 0.0,
            0.0, 0.0, '2015-09-21', '13:05:56', '13:09:00'],
           ['Chris', 'coffee + hot water', 0, 1.0, 0.0, 0, 0, 0.0, 0.0, 0.0,
            0.0, 0.0, '2015-09-21', '13:05:58', '13:09:00'],
           ['Chris', 'coffee + hot water', 0, 1.0, 0.0, 0, 0, 0.0, 0.0, 0.0,
            0.0, 0.0, '2015-09-21', '13:05:59', '13:09:00']]

  df=pd.DataFrame(data,columns=cols)

The desired output would look like:所需的输出如下所示:

data_out=[['Chris','coffee + hot water','0','0','0','0','0','0','1','0','0','0','2015-09-21','13:05:54','13:05:56','13:09:00'],['Chris','coffee + hot water','0','1','0','0','0','0','0','0','0','0','2015-09-21','13:05:58','13:05:59','13:09:00']]

cols_out=['User',
 'Activity',
 'Coaster1',
 'Coaster2',
 'Coaster3',
 'Coaster4',
 'Coaster5',
 'Coffee',
 'Door',
 'Fridge',
 u'coldWater',
 'hotWater',
 'SensorDate',
 'SensorTimeFirst',
'SensorTimeLast',
 'RegisteredTime']


df_out=pd.DataFrame(data_out, columns=cols_out)

You can try groupby and them apply custom function f like:您可以尝试groupby ,它们会apply自定义函数f例如:

def f(x):
    Doormin = x[x['Door'] == 1].min()
    Doormax = x[x['Door'] == 1].max()
    Coaster2min = x[x['Coaster2'] == 1].min()
    Coaster2max = x[x['Coaster2'] == 1].max()    
    Coaster1min = x[x['Coaster1'] == 1].min()
    Coaster1max = x[x['Coaster1'] == 1].max()      
    Door = pd.Series([Doormin['Door'], Doormin['SensorDate'], Doormin['SensorTime'], Doormax['SensorTime'], Doormin['RegisteredTime']], index=['Door','SensorDate','SensorTimeFirst','SensorTimeLast','RegisteredTime'])
    Coaster1 = pd.Series([Coaster1min['Coaster1'], Coaster1min['SensorDate'], Coaster1min['SensorTime'], Coaster1max['SensorTime'], Coaster1min['RegisteredTime']], index=['Coaster1','SensorDate','SensorTimeFirst','SensorTimeLast','RegisteredTime'])
    Coaster2 = pd.Series([Coaster2min['Coaster2'], Coaster2min['SensorDate'], Coaster2min['SensorTime'], Coaster2max['SensorTime'], Coaster2min['RegisteredTime']], index=['Coaster2','SensorDate','SensorTimeFirst','SensorTimeLast','RegisteredTime'])

    return pd.DataFrame([Door, Coaster2, Coaster1])

print df.groupby(['User','Activity']).apply(f)

                            Coaster1  Coaster2  Door RegisteredTime  \
User  Activity                                                        
Chris coffee + hot water 0       NaN       NaN     1       13:09:00   
                         1       NaN         1   NaN       13:09:00   
                         2       NaN       NaN   NaN            NaN   

                            SensorDate SensorTimeFirst SensorTimeLast  
User  Activity                                                         
Chris coffee + hot water 0  2015-09-21        13:05:54       13:05:56  
                         1  2015-09-21        13:05:58       13:05:59  
                         2         NaN             NaN            NaN  

And maybe you can add 0 instead of NaN by fillna :也许您可以通过fillna添加0而不是NaN

df = df.groupby(['User','Activity']).apply(f)
df[['Coaster1','Coaster2','Door']] = df[['Coaster1','Coaster2','Door']].fillna(0)
print df
                            Coaster1  Coaster2  Door RegisteredTime  \
User  Activity                                                        
Chris coffee + hot water 0         0         0     1       13:09:00   
                         1         0         1     0       13:09:00   
                         2         0         0     0            NaN   

                            SensorDate SensorTimeFirst SensorTimeLast  
User  Activity                                                         
Chris coffee + hot water 0  2015-09-21        13:05:54       13:05:56  
                         1  2015-09-21        13:05:58       13:05:59  
                         2         NaN             NaN            NaN  

You can use the below function.您可以使用以下功能。 You'll get the frequency of all items.您将获得所有项目的频率。 data.value_counts()

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

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