๐คณ ์ ๋ฒ pandas tricks ์๊ฐ์๋ display ํํ๋ฅผ ์ด๋ป๊ฒ ๋ณด์ด๊ฒ ํ ๊ฒ์ธ์ง์ ๋ํด ๋ฐฐ์ ์๋ค. ์ข ๋ flexibleํ ๊ฐ๋ ฅํ dataframe styling์ ๋ํด ์์๋ณด๋ ค ํ๋ค!
→ pandas์๋ ์ด๋ฏธ ์ฃผ์ด์ง dataframe ๋๋ series๋ฅผ CSS๋ฅผ ์ด์ฉํ์ฌ cutomized๋, ๋ค์ํ ๋์์ธ + ํ์ ์ต์ ์ ์ ๊ณตํด์ค๋ค.
โ dictionary ํํ๋ก format์ ์ํ๋ column๋ช ๊ณผ ๋ฐ๋ style์ ์ง์ ํ ์ฌ๋ฌ pair๋ค์ ์ง์ด๋ฃ์ ์ ์๋ค
โก dataframe ์ ์ฒด์์ ์ ์ฉ์ํค๋ ์ฌ๋ฌ method๋ค์ ์ง์ด๋ฃ์ ์ ์๋ค
(์๋ฅผ ๋ค๋ฉด hide_index() ๋ผ๋๊ฐ set_caption(), bar() ๋ฑ๋ฑ)
pandas Tricks_13๐๐ป 'change display options' (Kevin by DataSchool)
๐บ pandas & dataframe์ ๋๋ ์ผ ๋ ์ ์๋ ๊ด๊ณ! ๐บ โช์ฃผ์ด์ง dataframe์ด ์ก์์ ์ด๋ป๊ฒ ๋ณด์ด๊ฒ ํ๋ ์ง ์ฌ๋ฌ ์ค์ ์ ํตํด ๋ฐ๊ฟ๋ณด์โซ โป ์ฌ๊ธฐ์ ์ค์ํ๊ฑด, data๋ internallyํ๊ฒ ๋ฐ๋์ง ์๋๋ค๋ ์ (์ก
sh-avid-learner.tistory.com
๐คน๐ฟโ๏ธ ๊ด๋ จ docu ๐คน๐ฟโ๏ธ
.formats.style.Styler() docu
https://pandas.pydata.org/docs/reference/api/pandas.io.formats.style.Styler.html
- ์ด๋ฒ์๋ ์ด๊น์์ด titanic ์์ -
โ ๋จผ์ ์ธ์์ ์ง์ด๋ฃ์ format_dict ํํ๋ฅผ ๋ง๋ค์ด ๊ฐ ์ธ๋ถ column๋ณ ์ด๋ป๊ฒ ํํํ ์ง ์ค์ ํ๋ค
format_dict = {'age': '{:.0f}', #๋์ด๋ ์์ฐ์๋ก
'fare': '${:.3f}',
'class': lambda x:x.lower()
}
โก ์ง์ dataframe.style.format()์ดํ์ chaining method๋ก ์ง์ด๋ฃ์ ์ฌ๋ฌ method๋ฅผ ๊ฐ๋ค ๋ถ์
(titanic.style.format(format_dict)
.hide(axis='index')
.background_gradient(subset ='fare', cmap='YlOrBr')
.highlight_max('pclass', color='yellow')
.highlight_null(subset = 'age', null_color='black')
.bar('age', color='lightyellow', align='zero')
.set_caption('Titanic Passengers data')
)
- ์์ฑ! -
→ age๋ ์ ์ํ, fare๋ ์์ $๋ฅผ ๋ถ์ด๊ณ ์์์ ์ ์งธ ์๋ฆฌ๊น์ง ํํ, class๋ ๋ชจ๋ ๋ฌธ์ ์๋ฌธ์ํ์ผ๋ก ๋ฐ๊ฟ (dict ํํ๋ก ์ง์ด๋ฃ์)
→ hide๋ก index column์ display ์ํ๊ฒ ์ค์
→ background_gradient๋ก fare๊ฐ ๋์์๋ก ์ข ๋ ์งํ ๋ ธ๋์์ผ๋ก ํํ
→ highlight_max๋ก pclass ์ ์ผ ๋ฎ์ ๋ฑ๊ธ(์ซ์๋ก๋ ๋์๊ฒ ์ฌ๊ธฐ์๋ ์ ์ผ ๋ฎ์ ๋ฑ๊ธ)์ ๋ ธ๋์ ์์น
→ highlight_null๋ก age๊ฐ ํ์๋์ง ์์ ์น๊ฐ์ ๊ฒ์ ์ ๋ฐฐ๊ฒฝ
→ bar๋ก age ์ ๊ณ ๋ง์์ ๋ท๋ฐฐ๊ฒฝ ์๊น ๊ธธ์ด๋ก ํํ
→ set_caption์ผ๋ก ํด๋น dataframe์ ์ค๋ช ์ ์์ ๋ถ์
- ์ด ์ธ์๋ ์ ๋ง ๋ง์ ์ค์ option๋ค์ด ์์ผ๋ฏ๋ก ์ docu()์์ ๋ค์ด๊ฐ ์ ๋ง์ ๋ง๊ฒ displayํด๋ณด์! -
โป ์ค์ data๋ฅผ internallyํ๊ฒ ๋ฐ๊พธ์ง๋ ๋ชปํ์ง๋ง, โ EDA๊ณผ์ ์์ ์ง๊ด์ ์ผ๋ก data๋ฅผ ์ดํดํ๋ ๊ณผ์ , โก๋ฐํ์์ ์ ์ฅ์์ ์ข ๋ ์ค๋๋ ฅ์ ์๋, ์ ๋ฌ๋ ฅ ์๊ฒ ์ค๋ช ์ ์ ๋ฌํ๋ ๊ณผ์ - ์ด ๋ ๊ณผ์ ์ ์ํด styling์ ์ ์ฌ์ ์๋ก ์ฐ์ผ ์ ์์ โป
* ์ถ์ฒ1) https://towardsdatascience.com/style-pandas-dataframe-like-a-master-6b02bf6468b0
* ์ถ์ฒ2) https://www.youtube.com/watch?v=RlIiVeig3hc
'Python > Pandas&Numpy' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Numpy fundamentals 2/2 (0) | 2023.01.16 |
---|---|
pandas Tricks (Kevin by DataSchool) ์๋ฃ! COMPILATION (0) | 2022.04.18 |
pandas Tricks_13๐๐ป 'change display options' (Kevin by DataSchool) (0) | 2022.04.17 |
pandas Tricks_12๐๐ป 'pivot_table()' (Kevin by DataSchool) (0) | 2022.04.14 |
pandas Tricks_11๐๐ป 'Reshaping โ stack() & unstack()' (Kevin by DataSchool) (0) | 2022.04.14 |
๋๊ธ