【问题标题】:How to Group and get only the records with consecutive rank based on a condition in Python Pandas or SQL如何根据 Python Pandas 或 SQL 中的条件对具有连续排名的记录进行分组和获取
【发布时间】:2022-01-25 15:39:38
【问题描述】:

以下是我拥有的数据,共有 3 列:

  1. ID - 会员 ID
  2. 公司:公司名称
  3. 年份 - 入职年份
import pandas as pd
import numpy as np

data = {'ID':[1,1,1,2,2,3,3,3,3,3,4,4,4],
        'Company':['Google','Microsoft','LinkedIn','Youtube','Google','Google','Microsoft','Youtube','Google','Microsoft','Microsoft','Google','LinkedIn'],
        'Year':[2001,2004,2009,2001,2009,1999,2000,2003,2006,2010,2010,2012,2020]}

FullData = pd.DataFrame(data)


FullData - 
ID  Company   Year
1   Google    2001
1   Microsoft 2004
1   LinkedIn  2009
2   Youtube   2001
2   Google    2009
3   Google    1999
3   Microsoft 2000
3   Youtube   2003
3   Google    2006
3   Microsoft 2010
4   Microsoft 2010
4   Google    2012
4   LinkedIn  2020

下面我按照ID对数据进行了分组,按照年份排序


FullData['Rank'] = FullData.groupby('ID')['Year'].rank(method='first').astype(int)
FullData


ID  Company    Year    Rank
1   Google     2001     1
1   Microsoft  2004     2
1   LinkedIn   2009     3
2   Youtube    2001     1
2   Google     2009     2
3   Google     1999     1
3   Microsoft  2000     2
3   Youtube    2003     3
3   Google     2006     4
3   Microsoft  2010     5
4   Microsoft  2010     1
4   Google     2012     2
4   LinkedIn   2020     3

现在我只需要获取在 google 之后立即加入 Microsoft 的 Member ID。我只需要获取按 ID 分区或分组的记录,其中包含 Company Google 和 Microsoft,并且 Google 的排名紧随 Microsoft 之后。 (接受的输出 --> 谷歌 - 排名 1 和微软 - 排名 2 或 Google - Rank 4 和 Microsoft -Rank 5 等等..)

以下是所需输出的示例

ID  Company    Year    Rank
1   Google     2001     1
1   Microsoft  2004     2
3   Google     1999     1
3   Microsoft  2000     2
3   Google     2006     4
3   Microsoft  2010     5

或唯一 ID 的计数

Count of Unique ID's/Members who worked for Google prior to Microsoft = 2

感谢任何帮助。提前一百万谢谢

【问题讨论】:

    标签: sql python-3.x pandas dataframe pandas-groupby


    【解决方案1】:

    使用布尔索引:

    def myfunc(df):
        m1 = (df['Company'].eq('Google') & df['Company'].shift(-1).eq('Microsoft'))
        m2 = (df['Rank'].eq(df['Rank'].shift(-1) - 1))
        return df[(m1 & m2) | (m1.shift() & m2.shift())]
    
    out = FullData[FullData['Company'].isin(['Google', 'Microsoft'])] \
              .groupby('ID').apply(myfunc).droplevel(0)
    print(out)
    
    # Output:
       ID    Company  Year  Rank
    0   1     Google  2001     1
    1   1  Microsoft  2004     2
    5   3     Google  1999     1
    6   3  Microsoft  2000     2
    8   3     Google  2006     4
    9   3  Microsoft  2010     5
    

    对于唯一计数,使用out['ID'].nunique()

    【讨论】:

    • 感谢您的所有帮助,请您帮助我了解您的解决方案中的“m”是什么。因为它给了我一个错误,说 m 没有定义。
    • 我修复了我的解决方案。你能检查一下吗?现在可以用了吗?
    • 它现在工作完美。再次感谢所有帮助。我真的很感激:)
    • 我的答案不再有效?为什么现在拒绝我的回答?
    • 很抱歉,这是错误的,我点击了很多次。对于那个很抱歉。效果很好。
    猜你喜欢
    • 2023-01-26
    • 2021-12-05
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    相关资源
    最近更新 更多