从Python数据帧列中获取n个最大数字

问题描述 投票:0回答:1
# to get 3 largest numbers from column duration with all duplicates
import pandas as pd
technologies =  "A B C D E F G H I J".split()
duration     = [88,70,50,87,77,88,77,88,1,87]
columns      = ['technologies','duration']
df = pd.DataFrame(list(zip(technologies,duration)), columns=columns)

print(df.nlargest(3, 'duration',keep = "all"))

输出:

      technologies  duration
0            A        88
5            F        88
7            H        88

所需输出:

      technologies  duration
0            A        88
5            F        88
7            H        88
3            D        87
9            J        87
4            E        77
6            G        77
pandas max
1个回答
0
投票

用途:

top = (df.sort_values('duration', ascending=False)
         .pipe(lambda x: x[x['duration'].isin(x['duration'].unique()[:3])]))
print (top)
  technologies  duration
0            A        88
5            F        88
7            H        88
3            D        87
9            J        87
4            E        77
6            G        77
© www.soinside.com 2019 - 2024. All rights reserved.