A super simple question, which I cannot find so far.
This is my 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
id Name Lastname Score
0 1 Joe Doe 17.5
1 2 Bill Foo 8.0
CodePudding user response:
If same values per id
in Name
and Lastname
columns add it to 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:
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'})