简体   繁体   English

基于groupby的DataFrame过滤器

[英]DataFrame filter based on groupby

Here is my simplified example df: 这是我的简化示例df:

      salesPerson customer measure timeStamp
      --------------------------------------
      A           123      I       12:30
      A           123      II      12:30
      A           123      III     12:30
      B           123      IV      12:35
      C           456      I       14:30
      C           456      II      14:30
      D           456      III     14:15

What I want to do, it to filer the dataframe and in cases when 2 different salesPerson Id's have the same customer number, keep all the rows of the salesPerson whos timeStamp is the earliest. 我要执行的操作是归档数据帧,并在2个不同的salesPerson ID具有相同客户编号的情况下,请保留timeStamp最早的salesPerson的所有行。 Resulting df in this example would be: 在此示例中,结果df为:

      salesPerson customer measure timeStamp
      --------------------------------------
      A           123      I       12:30
      A           123      II      12:30
      A           123      III     12:30
      D           456      III     14:15

What would be the best/most pythonic way to do it? 最好/最有效的方法是什么? I thought about using pandas groupby.filter or groupby.transform, but frankly have no idea how to accurately write those. 我考虑过使用熊猫groupby.filter或groupby.transform,但是坦率地说,不知道如何准确地编写它们。

Bonus points would be for having the deleted rows in a separate deleted_df object. 奖励点是将删除的行放在单独的Deleted_df对象中。

This one-liner should do the trick: 这种单线应该可以解决问题:

df[df['salesPerson'].isin(df.iloc[df.groupby(['customer'])['timeStamp'].idxmin(), 'salesPerson'])]

Explanation: 说明:

To determine the salespersons to whom we want to filter, first group df by customer and get the index where the minimum timeStamp is found using idxmin : 为了确定我们要过滤的销售人员,首先按customerdf进行分组,并使用idxmin timeStamp找到最小timeStamp的索引:

df.groupby(['customer'])['timeStamp'].idxmin()

Then, pass those index values to iloc , along with the column we want, to get the values from salesPerson we'll use for filtering: 然后,将这些索引值以及iloc ,以从用于过滤的salesPerson获取值:

df.iloc[df.groupby(['customer'])['timeStamp'].idxmin(), 'salesPerson']

Finally, pass that result to the Series method isin , and use that to index into df . 最后,将该结果传递给Series方法isin ,并使用该结果索引到df The result is thus: 结果是:

0  A  123    I 2017-07-12 12:30:00
1  A  123   II 2017-07-12 12:30:00
2  A  123  III 2017-07-12 12:30:00
6  D  456  III 2017-07-12 14:15:00

To create a second DataFrame with the filtered-out rows, you could pass the index from the filtered df to the original df and exclude those rows. 要创建带有已过滤出行的第二个DataFrame,可以将索引从已过滤df传递到原始df,并排除这些行。 So if we assigned the result above to df1 , we could create a complementary df2 in this manner: 因此,如果我们将上述结果分配给df1 ,则可以按以下方式创建互补的df2

df2 = df[~df.index.isin(df1.index)]

Result: 结果:

3  B  123  IV 2017-07-12 12:35:00
4  C  456   I 2017-07-12 14:30:00
5  C  456  II 2017-07-12 14:30:00

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

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