* 실제 사용되는 데이터는 하나의 완벽한 dataset이 아닌 여러 개로 쪼개진 경우가 많다!
→ 따라서 분석하기 용이하게 하나의 dataset으로 만들기 위한 과정이 필요
1. Tidy Data란?
"각 변수가 열이고 각 관측치가 행으로 배열된 data (by Hadley Wickham)"
- columns에 있던 data를 row로 녹인다고 생각하면 쉬움!
- seaborn 시각화할 때 짧은 코드로 가능! (매우 편리)
↓↓↓↓ box plot 시각화할 때 사용한 tidy data 예 살펴보기 ↓↓↓↓
violin plot (+seaborn)
* EDA에 대해 알아보았고 EDA 과정에서 많이 쓰이는 시각화 방법 중 box plot에 대해 자세히 알아보았다. box plot (+seaborn) * 저번 EDA 개념 포스팅에서 EDA가 무엇인지 알아보았고, data 종류별 & 상황별
sh-avid-learner.tistory.com
- 분석하기 좋은 데이터
"a standard way of mapping the meaning of a dataset to its structure. A dataset is messy or tidy depending on how rows, columns and tables are matched up with observations, variables and types."
≫ every column is a VARIABLE
≫ every row is an OBSERVATION
≫ every cell is a SINGLE VALUE
→ 즉, 쉽게 정리하면 '모든 feature들의 조합에 맞는 각각의 관측치가 한 행씩 쭉 나열된 걸 tidy data'라 한다
- country와 year의 조합별 한 개씩의 value들이 한 행씩 나열되어 있다! -
2. melt()
"messy data → tidy data"
☝️ melt docu() ☝️
https://pandas.pydata.org/docs/reference/api/pandas.melt.html
pandas.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name='value', col_level=None, ignore_index=True)
"Unpivot a DataFrame from wide to long format, optionally leaving identifiers set. This function is useful to massage a DataFrame into a format where one or more columns are identifier variables (id_vars), while all other columns, considered measured variables (value_vars), are “unpivoted” to the row axis, leaving just two non-identifier columns, ‘variable’ and ‘value’."
id_vars & value_vars
→ id_vars는 기존 index, 즉 기존의 dataframe에서 행 별로 나누는 일종의 id값이라 보면 된다
→ value_vars는 기존 dataframe에서 각종 모든 feature들의 조합이라 보면 됨
var_name & value_name
→ var_name은 value_vars의 이름, value_name은 각 행 별 최종 관측값 column의 이름!
ex)
* 예를 들어 아래와 같은 table이 있다 하면
table1 = pd.DataFrame(
[[np.nan, 2],
[16, 11],
[3, 1]],
index=['X', 'Y', 'Z'],
columns=['A', 'B'])
* pivot 실행 결과
tidy1 = table1.reset_index().melt(id_vars = 'index', value_vars = ['A', 'B'])
- tidy한 형태로 변환! index와 variable의 조합별 value값이 나와있다 -
3. pivot_table()
"tidy data → messy data"
☝️ pivot_table() docu ☝️
https://pandas.pydata.org/docs/reference/api/pandas.pivot_table.html
pandas.pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', fill_value=None, margins=False, dropna=True, margins_name='All', observed=False, sort=True)
"Create a spreadsheet-style pivot table as a DataFrame. The levels in the pivot table will be stored in MultiIndex objects (hierarchical indexes) on the index and columns of the result DataFrame."
→ tidy data를 messy data로 원위치시키는 함수로 사용하기도 하지만, 원본 dataframe에서 추가적으로 유용한 정보만 골라 dataframe의 subset으로 만들고 싶을 때 사용하기도 하는 함수..!
→ 멀티인덱스로도 만들 수 있어 자유자재로 dataframe을 만들 때 사용 가능하다..!
** 출처)
https://garrettgman.github.io/tidying/
https://cfss.uchicago.edu/notes/tidy-data/
https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html
'Computer Science > Concepts' 카테고리의 다른 글
REST API example - Coingecko API (0) | 2022.03.30 |
---|---|
Tabular Data 🗄️ (0) | 2022.03.26 |
Data Preprocessing (0) | 2022.03.25 |
FE - Feature Engineering (0) | 2022.03.22 |
EDA - Exploratory Data Analysis (0) | 2022.03.22 |
댓글