Pandas group_by 保留顺序

3

我有一些数据看起来像这样:

    Season  Team    TEAM_ID start   end
0   1984-85 CHI 1610612741  1984    1985
1   1985-86 CHI 1610612741  1985    1986
2   1986-87 CHI 1610612741  1986    1987
3   1987-88 CHI 1610612741  1987    1988
4   1988-89 CHI 1610612741  1988    1989
5   1989-90 CHI 1610612741  1989    1990
6   1990-91 CHI 1610612741  1990    1991
7   1991-92 CHI 1610612741  1991    1992
8   1992-93 CHI 1610612741  1992    1993
9   1994-95 CHI 1610612741  1994    1995
10  1995-96 CHI 1610612741  1995    1996
11  1996-97 CHI 1610612741  1996    1997
12  1997-98 CHI 1610612741  1997    1998
13  2001-02 WAS 1610612764  2001    2002
14  2002-03 WAS 1610612764  2002    2003

我正在寻找一种将团队和团队id列分组并获取最小开始值和最大结束列的方法。对于上述数据,结果应该是:
Team    TEAM_ID    Years
CHI 1610612741  1984-93
CHI 1610612741  1994-98
WAS 1610612764  2001-03

对于一个一年内拥有多个团队的人来说,

    Season  Team    TEAM_ID start   end
0   2003-04 MIA 1610612748  2003    2004
1   2004-05 MIA 1610612748  2004    2005
2   2005-06 MIA 1610612748  2005    2006
3   2006-07 MIA 1610612748  2006    2007
4   2007-08 MIA 1610612748  2007    2008
5   2008-09 MIA 1610612748  2008    2009
6   2009-10 MIA 1610612748  2009    2010
7   2010-11 MIA 1610612748  2010    2011
8   2011-12 MIA 1610612748  2011    2012
9   2012-13 MIA 1610612748  2012    2013
10  2013-14 MIA 1610612748  2013    2014
11  2014-15 MIA 1610612748  2014    2015
12  2015-16 MIA 1610612748  2015    2016
13  2016-17 CHI 1610612741  2016    2017
14  2017-18 CLE 1610612739  2017    2018
15  2017-18 MIA 1610612748  2017    2018
17  2018-19 MIA 1610612748  2018    2019

我很愿意将其呈现为这样:
Team   TEAM_ID Years
MIA 1610612748  2003-16
CHI 1610612741  2016-17
CLE 1610612739  2017-17
MIA 1610612748  2017-19

有谁知道怎么做吗?我尝试使用 pandas.group_by,但它会将相同的团队分为一组,而我想保持它们分开。


1
Please show your attempt. - Philip
所以你基本上想按季节分组,但输出的季节是年份? - wwnde
@wwnde 是的,但如果我使用groupby,它会将所有团队分组在一起,在我的第二个示例中,我想保持每个MIA工作期间的独立性。 - Ethan
请发布您已经尝试过的内容,我们可以看到问题所在并加以改进。 - wwnde
对于第二个数据框,y 在 2017-2018 年期间未运行。 - sammywemmy
@sammywemmy 这是因为球员在2017赛季中被交易了。 - Ethan
2个回答

3

一种方法是使用嵌套的groupby来识别团队内连续赛季:

def func(df):
    # indicator of consecutive seasons
    g = (df['start'] > df['end'].shift(1)).cumsum()
    res = df.groupby(g).apply(
        lambda x: str(x['start'].min()) + '-' + str(x['end'].max())[-2:],
    )
    res.name = 'Years'
    return res 


df.groupby(['Team', 'TEAM_ID']).apply(func).reset_index()[['Team', 'TEAM_ID', 'Years']]

输出:

  Team     TEAM_ID    Years
0  CHI  1610612741  2016-17
1  CLE  1610612739  2017-18
2  MIA  1610612748  2003-16
3  MIA  1610612748  2017-19

太好了!感谢你的帮助。 - Ethan

2

对于您的问题,另一个解决方案是:它使用Pandas的shift方法来查找行之间的差异,并使用groupby函数。

 def grouping(df):

    #condition checks if row - previous row is not equal to 1 (end column)
    #or row not equal to previous row for the Team column
    cond = df.end.sub(df.end.shift()).ne(1) | (df.Team.ne(df.Team.shift()))

    #get rows where the end year does not change
    no_year_end_change = df.end.shift(-1).sub(df.end).eq(0)

    #create a new column to get values from the start column based on the condition
    df['change'] = df.loc[cond,'start']

    #create a new column to get values from the end column based on the condition
    df['end_edit'] = np.where(no_year_end_change,df.start,df.end)

    #integer conversion... gets rids of the float 0s
    df['change'] = df.change.ffill().astype('Int64')

    #groupby, get the max of the end column
    df = df.groupby(['Team','TEAM_ID','change']).end_edit.max().reset_index()

    #combine change and end columns using Pandas' str cat function
    df['Years'] = df.change.astype(str).str.cat(df.end_edit.astype(str),sep='-')
    df = df.drop(['change','end_edit'],axis = 1)

    return df

第一个数据框:

 df.pipe(grouping)
     Team   TEAM_ID      Years
0   CHI     1610612741  1984-1993
1   CHI     1610612741  1994-1998
2   WAS     1610612764  2001-2003

第二个数据框:

df1.pipe(grouping)

   Team      TEAM_ID      Years
0   CHI     1610612741  2016-2017
1   CLE     1610612739  2017-2017
2   MIA     1610612748  2003-2016
3   MIA     1610612748  2017-2019

这太棒了!谢谢你。 - Ethan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接