pandas Series/DataFrame获取n个最大值(largest values)和n个最
发布时间:2020-12-17 17:02:50 所属栏目:Python 来源:网络整理
导读:使用pandas时,想要获取一个序列中最大的N个值,和最小N个数值 经过查阅,pandas自带两个方法可以直接获取最大:nlargest和最小:nsmallest。 nlargest https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.nlargest.html#pandas.Ser
使用pandas时,想要获取一个序列中最大的N个值,和最小N个数值 经过查阅,pandas自带两个方法可以直接获取最大:nlargest和最小:nsmallest。 nlargest https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.nlargest.html#pandas.Series.nlargest nsmallest https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.nsmallest.html#pandas.Series.nsmallest 使用样例: data?=?[ ????{"content":?"1",?"title":?"刘德华",?"info":?"",?"time":?1578877014},????{"content":?"2",?"time":?1579877014},????{"content":?"3",?"time":?1582877014},????{"content":?"12",?"title":?"苹果",?"time":?1582876014},????{"content":?"33",?"title":?"apple",?"time":?1581877014},????{"content":?"16",?"title":?"banana",?"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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |