如何从pandas.value\U计数对于每个变量?

2024-04-26 14:29:21 发布

您现在位置:Python中文网/ 问答频道 /正文

实际上,我只是想从pandas.value\U计数()功能?你知道吗

我尝试将apply函数与lambda函数一起使用,但没有成功:

print(match_won_by_team.apply(lambda x : match_won_by_team[x].index[0]))


remove_duplicate_match_codes = data.drop_duplicates(subset='match_code', keep='first').reset_index(drop=True)

match_won_by_team = remove_duplicate_match_codes.groupby('year')['winner'].value_counts()

print('Match won by each team in respective seasons:- ', match_won_by_team)

我希望输出显示2008年:拉贾斯坦邦皇家:13,2009年:德里敢死队:10等从系列。你知道吗

2008  Rajasthan Royals               13
      Kings XI Punjab                10
      Chennai Super Kings             9
2009  Delhi Daredevils               10
      Deccan Chargers                 9
      Royal Challengers Bangalore     9
2010  Mumbai Indians                 11
      Chennai Super Kings             9
      Deccan Chargers                 8

我在使用apply函数和lambda时遇到这个错误。 属性错误:'数字.int64'对象没有属性'index'


Tags: lambda函数indexbyvaluematchteamremove
1条回答
网友
1楼 · 发布于 2024-04-26 14:29:21

IIUC公司:

我认为你需要使用以下方法:

remove_duplicate_match_codes.groupby('year')['winner'].apply(lambda x: x.value_counts().head(1))

这将按年度对获奖者的每个部分应用值计数,并使用head检索第一条记录,或检索当年计数最多的获奖者。你知道吗

相关问题 更多 >