龙空技术网

python技巧 - 获取dataframe一列或列表中前n个最大值/最小值

程序化交易A2Z 238

前言:

当前各位老铁们对“python获取list中最大的值”都比较关注,朋友们都想要知道一些“python获取list中最大的值”的相关内容。那么小编在网摘上汇集了一些对于“python获取list中最大的值””的相关知识,希望小伙伴们能喜欢,小伙伴们快快来了解一下吧!

获取dataframe数值列中前3个最大值/最小值

示例代码1:

import pandas as pdimport timeimport numpy as np test_lst = [['A', 0, 6], ['A', 1, 57], ['A', 2, 81], ['A', 3, 9], ['A', 4, 87],['B', 0, 24], ['B', 1, 30], ['B', 2, 96], ['B', 3, 54], ['B', 4, 81], ['C', 0, 6], ['C', 1, 6], ['C', 2, 6], ['C', 3, 93], ['C', 4, 99], ['D', 0, 0], ['D', 1, 90], ['D', 2, 6], ['D', 3, 87], ['D', 4, 75], ['E', 0, 93], ['E', 1, 60], ['E', 2, 63], ['E', 3, 48], ['E', 4, 36]]test_df1 = pd.DataFrame(test_lst, columns = ["Name", "rating", "num_items"])list(test_df1.groupby('Name')['rating'].nlargest(3).index)

以上代码运行结果:

[('A', 4), ('A', 3), ('A', 2), ('B', 9), ('B', 8), ('B', 7), ('C', 14), ('C', 13), ('C', 12), ('D', 19), ('D', 18), ('D', 17), ('E', 24), ('E', 23), ('E', 22)]

示例代码2:

test_df2 = pd.DataFrame(data=np.random.randint(0, 10, (3, 10)), columns=range(10), index=range(3))  %time np.array([test_df2[c].nlargest(3).index.values for c in test_df2])

以上代码运行结果:

CPU times: total: 15.6 msWall time: 21.9 msarray([[0, 1, 2],       [1, 2, 0],       [0, 1, 2],       [0, 2, 1],       [2, 0, 1],       [0, 2, 1],       [0, 1, 2],       [0, 2, 1],       [1, 0, 2],       [0, 1, 2]], dtype=int64)
获取列表中前3个最大值/最小值
import heapqtest_list = [1, 3, 2, 4, 5]nlargest_elements = (heapq.nlargest(3, range(len(test_list)), test_list.__getitem__)) # nsmallest 最小值函数print(nlargest_elements)print([a[x] for x in nlargest_elements])

以上代码运行结果:

[4, 3, 1] #最大值的索引值[5, 4, 3] #前3最大值

标签: #python获取list中最大的值