Pandas dataframe에 테이블 제목을 추가하는 방법
matplotlib의 set_title() 함수를 사용하여 pandas DataFrame에서 생성된 테이블에 제목을 추가할 수 있습니다.
ax. set_title (' Some Title ')
다음 예에서는 이 기능을 실제로 사용하는 방법을 보여줍니다.
예: Pandas DataFrame에 테이블 제목 추가
다양한 농구 팀의 포인트와 어시스트를 표시하는 다음과 같은 팬더 DataFrame이 있다고 가정해 보겠습니다.
import pandas as pd #createDataFrame df = pd. DataFrame ({' team ': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], ' points ': [18, 22, 19, 14, 14, 11, 20, 28], ' assists ': [5, 7, 7, 9, 12, 9, 9, 4]}) #view DataFrame print (df) team points assists 0 to 18 5 1 B 22 7 2 C 19 7 3 D 14 9 4 E 14 12 5 F 11 9 6 G 20 9 7:28 a.m. 4
다음 코드를 사용하여 DataFrame의 값을 표시하는 matplotlib에 테이블을 만들고 set_title()을 사용하여 테이블 제목을 지정할 수 있습니다.
import matplotlib. pyplot as plt
#initialize figure
fig = plt. figure (figsize = (8, .2))
ax = fig. add_subplot (111)
#create table
ax. table (cellText = df. values , rowLabels = df. index ,
colLabels = df. columns , cellLoc=' center ')
#add title to table
ax. set_title (' Points and Assists by Team ')
#turn axes off
ax. axis (' off ')

참고 : 여기 에서 matplotlib의 table() 함수에 대한 전체 문서를 찾을 수 있습니다.
표 위에 “팀별 포인트 및 어시스트”라는 제목이 추가되었습니다.
또한 Fontdict 및 loc 인수를 사용하여 제목의 글꼴과 위치를 변경할 수 있습니다.
import matplotlib. pyplot as plt
#initialize figure
fig = plt. figure (figsize = (8, .2))
ax = fig. add_subplot (111)
#create table
ax. table (cellText = df. values , rowLabels = df. index ,
colLabels = df. columns , cellLoc=' center ')
#add title to table
ax. set_title (' Points and Assists by Team ',
fontdict={' fontsize ': 20 ,
' fontweight ': ' bold ',
' color ': ' steelblue '},
loc=' left ')
#turn axes off
ax. axis (' off ')

이제 제목 글꼴이 더 크고, 굵게, 왼쪽 정렬 및 파란색으로 변경되었습니다.
제목 모양을 변경할 수 있는 방법의 전체 목록은 matplotlib 설명서를 참조하세요.
추가 리소스
다음 튜토리얼에서는 Pandas에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.
Pandas에서 플롯에 제목을 추가하는 방법
Pandas DataFrame에서 포인트 클라우드를 만드는 방법
Pandas DataFrame에서 히스토그램을 만드는 방법