我有一个csv file that contains information of games played in the last 5 decades。我将寻找过去几年中每个赛季的冠军球队。
df3=pd.DataFrame(df_laliga.groupby('season')[['points']].max(axis=1).reset_index())
result=pd.merge(df3,df_laliga,how='inner',on=['points','season'])
result=result[['season','club']].set_index('season')
del result.index.name
结果采用这种格式
问题是在2006-07赛季,两支球队的积分相同,并且有两支球队,而该赛季没有一支,所以我也应该检查目标差值栏。我以下面的方式尝试了一下,但问题没有消失。
df3=pd.DataFrame(df_laliga.groupby('season')[['points','goal_difference']].max(axis=1).reset_index())
原始数据:
嗯,这可以使用pandasql轻松完成。您可以执行以下操作。
!pip install pandasql
import pandasql
query = """
SELECT
season,
club
FROM
(SELECT
season,
club,
points,
goal_difference
FROM
df_laliga
ORDER BY
season,
points,
goal_difference)
GROUP BY
season
"""
result = pandasql.sqldf(query)
result