본문 바로가기

Python_Wiki47

pandas: read_excel pd.read_excel(io, sheet_name=0, header=0, index_col=None)#df = pd.read_excel('등록인구_20250721135412.xlsx', header = 1) #1열을 컬럼명으로 만들어라#read_excel - header: 컬럼이름으로 사용할 영역 - index_col : 인덱스로 사용할 컬럼 지정 - usecols : 사용할 컬럼 지정#io는 우리가 입력한 경로 값#0으로 지정한 것은 위치 값, 두번째시트 불러오고싶으면 sheet_name = 1, 문자열 입력시 해당시트 불러오며, [0,1,"Sheet5"]처럼 여러개 선택하면 선택한 시트를 딕셔너리로 통합해 가져옴, None하면 모든 시트를 딕셔너리를 통합해 가져옴 #header=0도 위치 값이며,.. 2025. 7. 15.
Plotly 기초 / 스타일 설정하기 #주요 시각화 라이브러리#matplotlib: standard, seaborn: advanced version of matplotlib, plotly: new and interactive viz libimport plotly.express as pxfig = px.그래프종류(data_frame=데이터, x=X축 컬럼, y=Y축 컬럼, color=범례 컬럼, title=제목, labels=dict(X축 컬럼=X축 라벨, Y축 컬럼=Y축 라벨), width=그래프 가로길이, height=그래프 세로길이, text_auto=True/False)fig.show()#그래프종류: line, bar, box...#examplefig = px.bar(df_groupby, x='species', y='body_mass_.. 2025. 7. 14.
matplotlib / seaborn 고화질 설정 %config InlineBackend.figure_format = 'retina'#example%config InlineBackend.figure_format = 'retina'sns.barplot(data=df, x='species', y='body_mass_g')plt.title('펭귄의 종별 몸무게')plt.show() 2025. 7. 14.
matplotlib / seaborn 한글 적용하기 1. 한글폰트 설치 준비!sudo apt-get install -y fonts-nanum!sudo fc-cache -fv!rm ~/.cache/matplotlib -rf!sudo apt-get install -y fonts-nanum #sudo: 관리자 권한으로 실행 #-y: "예/아니오" 묻지 말고 자동 진행 #fonts-nanum 패키지를 리눅스 시스템에 설치!sudo fc-cache -fv #폰트 캐시를 초기화 및 갱신하는 명령어!rm ~/.cache/matplotlib -rf #matplotlib의 폰트 캐시를 삭제 2. 런타임 다시 시작3. 라이브러리 재 실행 (matplotlib 등)4. 한글 폰트 적용plt.rc('font', family='NanumGo.. 2025. 7. 14.
Seaborn 스타일 설정 sns.set_style(스타일) #스타일: darkgrid, whitegrid, dark, whitesns.set_palette(팔레트) #팔레트: 기본 세트는 아래 링크 참조#examplesns.set_style('white')sns.set_palette('flare')sns.barplot(data=df, x='species', y='body_mass_g')plt.show()공식문서 스타일 링크 Choosing color palettes — seaborn 0.13.2 documentationChoosing color palettes Seaborn makes it easy to use colors that are well-suited to the characteristics of your data .. 2025. 7. 14.
pandas: stack, unstack, melt (데이터 구조 변경) #stack : 컬럼 레벨에서 인덱스 레벨로 데이터프레임을 변경합니다.#unstack : 인덱스 레벨에서 컬럼 레벨로 데이터프레임을 변경합니다.pivot = pd.pivot_table(df, index=['Sex','Pclass'], values=['Survived','Fare'], aggfunc=['mean','median','sum'])pivot .stack(숫자, future_stack=True) #숫자만 쓰는것은 나중에 없어질 예정이라, future_stack=True를 꼭 같이 써주자.pivot.stack(0, future_stack=True) #컬럼의 첫번째 레벨을 인덱스로 내립니다.#0대신 1쓰면 두번째 레벨을 인덱스로 내림.unstack(숫자)pivot.unstack(1) #인덱스의 두번째.. 2025. 7. 14.