[英]Groupby() and mean() in pandas dataframe with returning more than two columns
A super simple question, which I cannot find so far.一个超级简单的问题,到目前为止我找不到。
This is my dataframe这是我的 dataframe
id Name Lastname Journal Article Score
0 1 John Doo Journal2 Article1 23
1 2 John Doo Journal1 Article2 12
2 3 Bill Foo Journal17 Article3 8
When I use当我使用
df.groupby('id', as_index=False)['Score'].mean()
it gives me它给了我
id Score
0 1 17.5
1 2 8.0
Expected output预期 output
id Name Lastname Score
0 1 Joe Doe 17.5
1 2 Bill Foo 8.0
If same values per id
in Name
and Lastname
columns add it to groupby
:如果Name
和Lastname
列中每个id
的相同值将其添加到groupby
:
df.groupby(['id','Name','Lastname'], as_index=False)['Score'].mean()
If possible different values per id
is possible extract first/last values per groups:如果可能,每个id
可能有不同的值,可以提取每个组的第一个/最后一个值:
df.groupby('id', as_index=False).agg({'Score':'mean', 'Name':'first', 'Lastname':'first'})
df.groupby('id', as_index=False).agg({'Score':'mean', 'Name':'last', 'Lastname':'last'})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.