我正在尝试获取系列中每组的最大值:
返回
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`
灵感来自How to group a Series by values in pandas? ,我尝试:
b = a.groupby(a).max()
return b
但是我丢失了原始索引值 0、1 和 3 的名称,奇怪的是,它们甚至没有被找到的最大值替换。
1
0.058824 0.058824
0.071429 0.071429
0.083333 0.083333
0.333333 0.333333
Name: 1, dtype: float64
有没有办法获取最大值,并在 groupby 之后保留索引值?
最佳答案
您需要指定系列的索引,而不是系列本身,作为 groupby
的参数:
a.groupby(a.index).max()
关于python - 系列的 Groupby 和 max,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53365544/