我有一些数据看起来像这样:
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
,但它会将相同的团队分为一组,而我想保持它们分开。