Python新手在这里。我试图了解 pandas groupby 和 apply 方法的工作原理。我找到了这个简单的例子,我把它贴在下面:
import pandas as pd
ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings',
'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'],
'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2],
'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017],
'Points':[876,789,863,673,741,812,756,788,694,701,804,690]}
df = pd.DataFrame(ipl_data)
数据框df
如下所示:
Team Rank Year Points
0 Riders 1 2014 876
1 Riders 2 2015 789
2 Devils 2 2014 863
3 Devils 3 2015 673
4 Kings 3 2014 741
5 kings 4 2015 812
6 Kings 1 2016 756
7 Kings 1 2017 788
8 Riders 2 2016 694
9 Royals 4 2014 701
10 Royals 1 2015 804
11 Riders 2 2017 690
到现在为止还挺好。然后我想转换我的数据,以便从每组团队中我只保留点列中的第一个元素。首先检查df['Points'][0]
确实给了我的第一个Points
元素df
,我尝试了这个:
df.groupby('Team').apply(lambda x : x['Points'][0])
认为函数的参数x
是lambda
另一个熊猫数据框。但是,python 会产生错误:
File "pandas/_libs/index.pyx", line 81, in pandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 89, in pandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 987, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 993, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 0
这似乎与 HashTable 有关,但我无法理解为什么。然后我想也许传递给的lambda
不是数据框,所以我运行了这个:
df.groupby('Team').apply(lambda x : (type(x), x.shape))
输出:
Team
Devils (<class 'pandas.core.frame.DataFrame'>, (2, 4))
Kings (<class 'pandas.core.frame.DataFrame'>, (3, 4))
Riders (<class 'pandas.core.frame.DataFrame'>, (4, 4))
Royals (<class 'pandas.core.frame.DataFrame'>, (2, 4))
kings (<class 'pandas.core.frame.DataFrame'>, (1, 4))
dtype: object
其中,IIUC 表明 的论点lambda
确实是一个熊猫数据框,其中包含每个团队的df
.
我知道我可以通过运行得到想要的结果:
df.groupby('Team').apply(lambda x : x['Points'].iloc[0])
我只是想从 apply 函数中了解为什么df['Points'][0]
有效而x['Points'][0]
不是无效。感谢您的阅读!