使用pandas时,想要获取一个序列中最大的N个值,和最小N个数值
经过查阅,pandas自带两个方法可以直接获取最大:nlargest和最小:nsmallest。
nlargest
nsmallest
使用样例:
data = [
{"content": "1", "title": "刘德华", "info": "", "time": 1578877014},
{"content": "2", "title": "刘德华", "info": "", "time": 1579877014},
{"content": "3", "title": "刘德华", "info": "", "time": 1582877014},
{"content": "12", "title": "苹果", "info": "", "time": 1582876014},
{"content": "33", "title": "apple", "info": "", "time": 1581877014},
{"content": "16", "title": "banana", "info": "", "time": 1561877014},
]
import pandas as pd
s = pd.Series(data)
# 最大3个值
print(pd.to_numeric(s.str.get('content'),errors='coerce').nlargest(3,keep='all'))
>>> 33,16,12
# 最小3个值
print(pd.to_numeric(s.str.get('content'),errors='coerce').nsmallest(3,keep='all'))
>>> 1,2,3
原文网址:
pandas Series/DataFrame获取n个最大值(largest values)和n个最小值((smallest values))