인프런 - 데이터 분석을 위한 판다스 - 섹션 5 - 테이블 형태 변경(Long to Wide)

르네·2023년 10월 17일
0

Python

목록 보기
39/45
post-thumbnail

본 내용은 인프런 강의 <데이터 분석을 위한 판다스>를 수강하며 중요한 점을 정리한 글입니다.

How to reshape the layout of tables

https://pandas.pydata.org/pandas-docs/stable/getting_started/intro_tutorials/07_reshape_table_layout.html#long-to-wide-table-format

import pandas as pd

titanic = pd.read_csv('https://raw.githubusercontent.com/pandas-dev/pandas/master/doc/data/titanic.csv')
titanic.head(2)

air_quality = pd.read_csv('https://raw.githubusercontent.com/pandas-dev/pandas/master/doc/data/air_quality_long.csv')
air_quality.head(2)

정렬하기

SELECT *
FROM titanic
ORDER BY Age
SELECT *
FROM titanic
ORDER BY Pclass DESC, Age DESC
titanic

titanic_sort_age = titanic.sort_values(by='Age')
titanic_sort_age = titanic_sort_age.reset_index()

titanic_sor_pclass_age = titanic.sort_values(by=['Pclass', 'Age'], ascending=False)

Long to wide table format (구글스프레드 시트 실습)


Long to wide table format (판다스 실습)

no2 = air_quality[air_quality['parameter'] == 'no2']

no2_subset = no2.sort_index().groupby(['location']).head(2)
no2_subset

no2_subset.pivot(columns='location', values='value')

no2.pivot(columns='location', values='value').plot(figsize=(12, 6))

profile
데이터분석 공부로그

0개의 댓글