[英]How to sort only positive data in Pandas Series that contains negative and zeros
我有一個帶有負數,零和正數的熊貓系列。 最簡單的方法是僅對正條目進行降序排序,然后從排序結果中返回前10位?
np.random.seed(123)
s = pd.Series(np.random.randint(-100,100,50))
s[s>0].sort_values(ascending=False).head(10)
輸出:
25 95
32 89
39 80
33 76
15 74
47 68
20 64
44 55
17 53
34 35
dtype: int32
將boolean indexing
與nlargest
一起nlargest
:
#borrowing data from Scott Boston
print (s[s>0].nlargest(10))
25 95
32 89
39 80
33 76
15 74
47 68
20 64
44 55
17 53
34 35
dtype: int32
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.