๊ด€๋ฆฌ ๋ฉ”๋‰ด

๋ชฉ๋กPYTHON (6)

Hey Tech

[Python] Pandas ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ๋ชจ๋“  ํ–‰, ์—ด ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•

๐Ÿ“š ๋“ค์–ด๊ฐ€๋ฉฐ๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” ํŒŒ์ด์ฌ pandas ์‚ฌ์šฉ ์‹œ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ์ „์ฒด ํ–‰๊ณผ ์—ด ์กฐํšŒ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค.๐Ÿ’ก์ƒํ™ฉํŒ๋‹ค์Šค ํ†ตํ•ด ์—‘์…€ ํŒŒ์ผ ์กฐํšŒ ์‹œ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ๋Š” ์ผ๋ถ€ ํ–‰๊ณผ ์—ด์ด ์ˆจ๊น€์ฒ˜๋ฆฌ ๋˜๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค(๊ทธ๋ฆผ 1).# ๋ฏธ์„ค์น˜ ์‹œ pip install pandasimport pandas as pddf = pd.read_excel(filepath+filename, sheet_name = 'Sheet2')df.head()โœ… ํ•ด๊ฒฐ๋ฐฉ๋ฒ•ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ํŒ๋‹ค์Šค ์˜ต์…˜์„ ๋ณ€๊ฒฝํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.1) ๋ชจ๋“  ์—ด ์ถœ๋ ฅ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋งŒ ์‹คํ–‰ํ•ด ์ฃผ๋ฉด ๋ชจ๋“  ์—ด์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(๊ทธ๋ฆผ 2).pd.set_option('display.max_columns', None)2) ๋ชจ๋“  ํ–‰ ์ถœ๋ ฅ์œ ์‚ฌํ•˜๊ฒŒ ๋ชจ๋“  ํ–‰ ์ถœ๋ ฅํ•˜๋Š” ๋ช…..

SW ๊ฐœ๋ฐœ/Python 2024. 5. 21. 11:08
[Python] matplotlib.pyplot.savefig ํŒŒ์ผ ์ €์žฅ ์‹œ ์—ฌ๋ฐฑ ์ œ๊ฑฐ ๋ฐฉ๋ฒ•

๐Ÿ‘จ‍๐Ÿ’ป 1.  ๊ฐœ์š”๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” ํŒŒ์ด์ฌ matplotlib.pyplot ํŒจํ‚ค์ง€๋ฅผ ํ™œ์šฉํ•œ ์‹œ๊ฐํ™” ์ž๋ฃŒ ์ €์žฅ ์‹œ ์ž๋ฃŒ ๋‚ด ๋ถˆํ•„์š”ํ•œ ์—ฌ๋ฐฑ์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋‹ค๋ฃน๋‹ˆ๋‹ค.๐Ÿค– 2.  ๋ฌธ์ œ์ ์•„๋ž˜ (๊ทธ๋ฆผ 1)๊ณผ ๊ฐ™์ด, matplotlib ์˜ต์…˜ ์„ค์ • ์—†์ด ์‹œ๊ฐํ™” ์ž๋ฃŒ๋ฅผ ์ €์žฅํ•˜๊ฒŒ ๋˜๋ฉด ๊ทธ๋ž˜ํ”„ ์ฃผ๋ณ€ ์‚ฌ๋ฐฉ์œผ๋กœ ๋ถˆํ•„์š”ํ•œ ์—ฌ๋ฐฑ์ด ๋งŽ์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ณด๊ณ ์„œ๋‚˜ ๋Œ€์‹œ๋ณด๋“œ ๋“ฑ์— ํ™œ์šฉ ์‹œ ๋‹ค๋ฅธ ์ฐจํŠธ๋“ค๊ณผ์˜ ์‹ฌ๋ฏธ์„ฑ ์žˆ๋Š” ์กฐํ™”๋ฅผ ๋ฐฉํ•ดํ•˜๋Š” ์š”์†Œ๊ฐ€ ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. [์˜ˆ์‹œ ์ฝ”๋“œ - savefig ๋ฉ”์†Œ๋“œ ์˜ต์…˜ ๋ฏธ์„ค์ •]# ํŒจํ‚ค์ง€ ์„ค์น˜ ํ•„์š” : pip install matplotlibimport matplotlib.pyplot as plt# ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑplt.plot([1, 2, 3, 4])plt.ylabel('some nu..

[Python] ์›” ๋‹จ์œ„ ๋ฐ์ดํ„ฐ ๊ทธ๋ฃนํ™” ๋ฐ ์‹œ๊ฐํ™”ํ•˜๊ธฐ

๐Ÿ’ก ๋“ค์–ด๊ฐ€๋ฉฐ ๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์›” ๋‹จ์œ„๋กœ ์นด์šดํŒ…ํ•˜๊ณ  ์ด๋ฅผ ์‹œ๊ฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. 1. ํŒจํ‚ค์ง€ import import pandas as pd import numpy as np import matplotlib.pyplot as plt 2. ๋ฐ์ดํ„ฐ์…‹ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ํฌ์ŠคํŒ…์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ์…‹์€ ๊ณต์œ ๊ฐ€ ๋ถˆ๊ฐ€ํ•˜์˜ค๋‹ˆ ํ•™์Šต ๋ชฉ์ ์œผ๋กœ๋งŒ ๋ด์ฃผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. DATASET = pd.read_csv('./dataset_app_review.csv') DATASET.head() 3. ๋ฐ์ดํ„ฐ ํƒ€์ž… ํ™•์ธ DATASET.dtypes ์•„๋ž˜์™€ ๊ฐ™์ด, ๋‚ ์งœ ์ •๋ณด(date)๋Š” ํ˜„์žฌ ์ •์ˆ˜ํ˜•(int)์ž…๋‹ˆ๋‹ค. ํŽธ๋ฆฌํ•˜๊ฒŒ ๋‚ ์งœํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์ด๋ฅผ datetime ํƒ€์ž…์œผ๋กœ ๋ฐ”๊ฟ”์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 4. ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ณ€๊ฒฝ "์—ฐ-..

[Python] ๋ฆฌ์ŠคํŠธ ๊ฐ„ ๋บ„์…ˆ(์ฐจ์ง‘ํ•ฉ) ๊ตฌํ˜„๋ฐฉ๋ฒ•

ํŒŒ์ด์ฌ์—์„œ 2๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ ๊ฐ„์— ๊ฒน์น˜๋Š” ์›์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•, ์ฆ‰ ์ฐจ์ง‘ํ•ฉ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด 2๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. list1 = ['a', 'b', 'c', 'd'] list2 = ['b', 'd'] โŒ ์ž˜๋ชป๋œ ๋ฐฉ๋ฒ• ๋ฆฌ์ŠคํŠธ๋ผ๋ฆฌ ํ•ฉ์ง‘ํ•ฉ์ธ ์›์†Œ ํ•ฉ์น˜๊ธฐ๋Š” ๋ง์…ˆ(+)์œผ๋กœ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ฐจ์ง‘ํ•ฉ์„ ์œ„ํ•œ ๋บผ์…ˆ์€ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. list1-list2 ๊ฒฐ๊ณผ โœ… ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ• ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜(List Comprehension)์œผ๋กœ ์‰ฝ๊ฒŒ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. sub_set = [x for x in list1 if x not in list2] ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…๋‹ˆ๋‹ค. sub_set ๊ฒฐ๊ณผ ํฌ์ŠคํŒ… ๋‚ด์šฉ์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์˜ค๋Š˜๋„..

SW ๊ฐœ๋ฐœ/Python 2022. 7. 4. 12:33