2024-06-30 08:31:49 发布
网友
This anwser说我可以使用df.groupby('id')['value'].nlargest(30)来获得每组的前30行。 但是如何让每一组的行从top2到top31?有没有一个函数可以做类似的事情,比如pandas.Series.nlargest
df.groupby('id')['value'].nlargest(30)
pandas.Series.nlargest
试着apply和.iloc得到,例如你想要1:30
apply
.iloc
df.sort_values('value').groupby('id').value.apply(lambda x : x.iloc[1:30])
试着
apply
和.iloc
得到,例如你想要1:30相关问题 更多 >
编程相关推荐