2025.02.24 - [프로그래밍/Python 관련 정보] - [Pandas] Table of Contents
Syntax는 아래와 같습니다.
DataFrame.nlargest(n, columns, keep='first')
DataFrame.nsmallest(n, columns, keep='first')
keep 옵션은 'first', 'last', 'all' 중 하나로 줄 수 있으며 n번째 데이터가 중복일 때 어떤 것을 리턴할 지 입력받는 argument입니다.
df2 = pd.DataFrame({'A': [10, 20, 20, 30, 30, 30], 'B': ['a', 'b', 'c', 'd', 'e', 'f']})
# 기본적으로 첫 번째 3개만 반환
print(df2.nlargest(3, 'A'))
# keep='last' 옵션 사용 (마지막 3개 반환)
print(df2.nlargest(3, 'A', keep='last'))
# keep='all' 옵션 사용 (중복 포함하여 모두 반환)
print(df2.nlargest(3, 'A', keep='all'))
A B
3 30 d
4 30 e
5 30 f
A B
1 20 b
2 20 c
3 30 d
A B
3 30 d
4 30 e
5 30 f
1 20 b
2 20 c
'프로그래밍 > Python 관련 정보' 카테고리의 다른 글
[Pandas] update (0) | 2025.02.23 |
---|---|
[Pandas] sort_index() / sort_values() (0) | 2025.02.23 |
[Python 기초] Broadcasting (0) | 2025.02.22 |
[Pandas] idxmax(), idxmin() (0) | 2025.02.20 |
[Algorithm] Dynamic Programming vs. Greedy Algorithm (0) | 2025.02.20 |