# 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
用途:
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