Pandas - 如果Criteria Met,则为groupby

时间:2017-06-20 13:45:53

标签: python pandas group-by pandas-groupby

以下数据基于货车的GPS坐标,点火开关是否以及货车在给定时间距目标位置的距离。我想确定一辆货车是在一个位置(或<300)处,是否关闭点火装置,以及两种情况是否都是真的,停留的持续时间。

在下面的示例中,我将行1-4视为“分组”在一起,因为它们是距离<300的连续行。第5行是“分组”的,因为它是> 300,而第6-8行是“分组”在一起的,因为它们是距离<300的连续行。

因此,由于在行1-4中关闭点火,我想计算持续时间(因为货车在该位置“停止”一段给定的时间)。但是,其他两组(第5行和第6-8行)不应该进行持续时间计算,因为在这些分组中点火从未关闭。

df
AcctID   On_Off    Distance  Timestamp
123      On        230       12:00
123      On        30        12:02
123      Off       29        12:05
123      Off       35        12:10
123      On        3000      12:13
123      On        100       12:20
123      On        95        12:22
123      On        240       12:28

我能够对距离是否小于300(Within_Distance)进行分类,但是确定分组中至少有一行的点火是否关闭让我感到难过。以下是最终数据框的样子:

df['Within_Distance'] = np.where(df['Distance']<300, "Yes", "No")

df
AcctID   On_Off    Distance  Timestamp   Within_Distance    Was_Off    Within_Distance_and_Was_Off
123      On        230       12:20       Yes                Yes        Yes
123      On        30        12:02       Yes                Yes        Yes
123      Off       29        12:05       Yes                Yes        Yes
123      Off       35        12:10       Yes                Yes        Yes
123      On        3000      12:13       No                 No         No
123      On        100       12:20       Yes                No         No
123      On        95        12:22       Yes                No         No
123      On        240       12:28       Yes                No         No

提前致谢!

2 个答案:

答案 0 :(得分:3)

试试吧:

df['Within_Distance'] = np.where(df['Distance']<300, "Yes", "No")

df['Was_Off'] = df.groupby((df.Distance > 300).diff().fillna(0).cumsum())['On_Off'].transform(lambda x: 'Yes' if (x == 'Off').any() else 'No')

df['Within_Distinace_and_Was_Off']  = np.where((df['Within_Distance'] == 'Yes') & (df['Was_Off'] == 'Yes'),'Yes','No')

输出:

   AcctID On_Off  Distance Timestamp Within_Distance Was_Off  \
0     123     On       230     12:00             Yes     Yes   
1     123     On        30     12:02             Yes     Yes   
2     123    Off        29     12:05             Yes     Yes   
3     123    Off        35     12:10             Yes     Yes   
4     123     On      3000     12:13              No      No   
5     123     On       100     12:20             Yes      No   
6     123     On        95     12:22             Yes      No   
7     123     On       240     12:28             Yes      No   

  Within_Distinace_and_Was_Off  
0                          Yes  
1                          Yes  
2                          Yes  
3                          Yes  
4                           No  
5                           No  
6                           No  
7                           No  

答案 1 :(得分:1)

首先,设置一个布尔字段以使用

df['Off'] = df['On_Off'] == 'Off'

然后构建一个标识groupby连续行的字段,如here

所示
(df['Within_Distance'] != df['Within_Distance'].shift()).cumsum()

并使用.any来识别groupby中任何行的布尔值为true:

df['Was_Off'] = df.groupby((df['Within_Distance'] != df['Within_Distance'].shift()).cumsum())['Off'].transform(any)
Out[31]: 
   AcctID On_Off  Distance Timestamp Within_Distance    Off  Was_Off
0     123     On       230     12:00             Yes  False     True
1     123     On        30     12:02             Yes  False     True
2     123    Off        29     12:05             Yes   True     True
3     123    Off        35     12:10             Yes   True     True
4     123     On      3000     12:13              No  False    False
5     123     On       100     12:20             Yes  False    False
6     123     On        95     12:22             Yes  False    False
7     123     On       240     12:28             Yes  False    False