파이썬 pandas 에서 sort 후 최대값 column 이름 알아내는 명령어 있을까요
조회수 2699회
pandas 에서 sort 후 최대값 column 이름 알아내는 명령어 있을까요 찾아봐도 안되서 막막하네요
index 0 3월 점수
index 1 4월 점수
3월점수와 4월 점수 중 성적이 많이 오른 과목을 찾는 방법을 알려주세요
과목 (column이름)
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
values 중에 최대값의 걸럼명을 알고 싶다는 거죠? (본문의 질문은 애매모호합니다...)
https://notebooks.azure.com/wincommerce/projects/hashcode/html/9226.ipynb
In [1]: import pandas as pd In [2]: scores = pd.np.random.randint(100, size=(5, 4)) df = pd.DataFrame(scores, columns=['국어', '영어', '수학', '과학']) In [3]: df Out[3]: 국어 영어 수학 과학 0 16 64 90 28 1 31 31 40 20 2 36 36 30 19 3 2 95 86 37 4 95 92 74 16 가장 큰 값이 95로 국어, 영어 두과목이다¶ In [4]: # 모든 과목중에 가장 큰 값들이 어디에 있는가? True 값을 확인하자. df == df.values.max() Out[4]: 국어 영어 수학 과학 0 False False False False 1 False False False False 2 False False False False 3 False True False False 4 True False False False In [7]: # 가장 큰 점수의 좌표를 얻자 ids, cols = pd.np.where(df == df.values.max()) In [8]: # idedx 와 column 이 각각 있으므로 확인하게 좋도록 튜틀로 만들었다. list(zip(ids, cols)) Out[8]: [(3, 1), (4, 0)] 95인 행:3 열:1 과 행:4 열:0 을 가리키고 있다.¶ In [9]: # column 명을 추출하자 [df.columns[c] for c in cols] Out[9]: ['영어', '국어']
댓글 입력