pandas DataFrame top N rows,某列值最大的N行

@创建于:2023.04.03
@修改于:2023.04.03

文章目录

    • 1、pandas.DataFrame.nlargest
    • 2、代码示例
    • 3、pandas.DataFrame.nsmallest

1、pandas.DataFrame.nlargest

pandas DataFrame top N rows,某列值最大的N行_第1张图片

2、代码示例

df = pd.DataFrame(
    {'population': [59000000, 65000000, 434000, 434000, 434000, 337000, 11300, 11300, 11300],
    'GDP': [1937894, 2583560, 12011, 4520, 12128, 17036, 182, 38, 311],
    'alpha-2': ["IT", "FR", "MT", "MV", "BN", "IS", "NR", "TV", "AI"]},
    index=["Italy", "France", "Malta", "Maldives", "Brunei", "Iceland", "Nauru", "Tuvalu", "Anguilla"])
df

          population      GDP alpha-2
Italy       59000000  1937894      IT
France      65000000  2583560      FR
Malta         434000    12011      MT
Maldives      434000     4520      MV
Brunei        434000    12128      BN
Iceland       337000    17036      IS
Nauru          11300      182      NR
Tuvalu         11300       38      TV
Anguilla       11300      311      AI


# 选择population最大的前3个,相同的值选择第一个
df.nlargest(3, 'population', keep="first")
        population      GDP alpha-2
France    65000000  2583560      FR
Italy     59000000  1937894      IT
Malta       434000    12011      MT

# 选择population最大的前3个,相同的值则都选择
df.nlargest(3, 'population', keep='all')
          population      GDP alpha-2
France      65000000  2583560      FR
Italy       59000000  1937894      IT
Malta         434000    12011      MT
Maldives      434000     4520      MV
Brunei        434000    12128      BN

# 指定多个列:按“population”列和“GDP”列中的最大值排序
df.nlargest(3, ['population', 'GDP'], keep="all")
        population      GDP alpha-2
France    65000000  2583560      FR
Italy     59000000  1937894      IT
Brunei      434000    12128      BN

3、pandas.DataFrame.nsmallest

pandas DataFrame top N rows,某列值最大的N行_第2张图片

# 指定多个列,按“population”列和“GDP”列中的最小值排序
df.nsmallest(3, ['population', 'GDP'])
          population  GDP alpha-2
Tuvalu         11300   38      TV
Nauru          11300  182      NR
Anguilla       11300  311      AI

你可能感兴趣的:(python,pandas,python)