ZakS
ZakS

Reputation: 1131

Groupby and max for a series

I am trying to obtain the maximum of each group in a Series:

return a

0
0    0.071429
0    0.071429
1    0.083333
1    0.333333
1    0.333333
1    0.083333
3    0.058824
3    0.058824
Name: 1, dtype: float64`

Inspired by How to group a Series by values in pandas?, I try:

b = a.groupby(a).max()
return b

But I lose the names of the original index values 0, 1 and 3, and oddly enough, they aren't even replaced by max values found.

1
0.058824    0.058824
0.071429    0.071429
0.083333    0.083333
0.333333    0.333333
Name: 1, dtype: float64

Is there a way to get the maximums, and keep the index values after the groupby?

Upvotes: 1

Views: 70

Answers (1)

mtripp100
mtripp100

Reputation: 241

You need to specify the Series' index, rather than the Series itself, as the argument to groupby:

a.groupby(a.index).max()

Upvotes: 3

Related Questions