2025.02.24 - [프로그래밍/Python 관련 정보] - [Pandas] Table of Contents
명령어 자체는 직관적인 명령어 입니다. 특정한 칼럼 기준으로 최대, 최소값을 갖도록하는 index를 찾은 후 그 index에 대응되는 다른 칼럼의 값을 찾을 때 사용할 수 있습니다. SQL에서 Keep을 이용한 용법과 유사한 측면이 있다고 볼 수 있겠네요.
import pandas as pd
# Series 예시
s = pd.Series([3, 7, 2, 9, 1])
print(s.idxmax())
# 결과: 3 (인덱스 3에서 최대값 9)
# DataFrame 예시
df = pd.DataFrame({
'A': [1, 5, 3],
'B': [4, 2, 6],
'C': [7, 8, 5]
}, index=['x', 'y', 'z'])
print(df.idxmax(axis=0)) # 열 방향 (기본값)
# 결과:
# A y
# B z
# C y
# dtype: object
print(df.idxmax(axis=1)) # 행 방향
# 결과:
# x C
# y C
# z B
# dtype: object
import pandas as pd
# 예시 데이터프레임
df = pd.DataFrame({
'A': [0.1, 0.5, 0.8, 0.3, 0.9],
'B': ['apple', 'banana', 'cherry', 'date', 'elderberry']
})
# A값이 최대가 되는 행의 B값 찾기
max_A_index = df['A'].idxmax() # A에서 최대값을 가지는 인덱스
max_B_value = df.loc[max_A_index, 'B'] # 해당 인덱스에서 B의 값
print(f"A값이 최대일 때 B값: {max_B_value}")
'프로그래밍 > Python 관련 정보' 카테고리의 다른 글
[Pandas] nlargest/nsmallest (0) | 2025.02.22 |
---|---|
[Python 기초] Broadcasting (0) | 2025.02.22 |
[Algorithm] Dynamic Programming vs. Greedy Algorithm (0) | 2025.02.20 |
[Algorithm] Dynamic Programming vs. Recursion (2) | 2025.02.19 |
[Pandas] Index를 이용한 DataFrame확장 (0) | 2025.02.18 |