๐Ÿ“’ Today I Learn/๐Ÿ Python

240513 Today I Learn๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐiris ๋ฐ์ดํ„ฐ์…‹์„ ํ™œ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌiris_data = sns.load_dataset('iris')iris_data.head()Q1. 'species' ์—ด ๊ฐ’์ด 'setosa'์ธ ๋ฐ์ดํ„ฐ ์„ ํƒํ•˜๊ธฐiris_data[iris_data['species']=='setosa']→ ์กฐ๊ฑด ์ธ๋ฑ์‹ฑํ•˜๊ธฐ dataframe[์กฐ๊ฑด]→ .loc๋ฅผ ํ™œ์šฉํ•  ์ˆ˜๋„ ์žˆ์Œ. `iris_data.loc[iris_data['species'] == 'setosa']`Q2. 10๋ถ€ํ„ฐ 20๊นŒ์ง€์˜ ํ–‰๊ณผ 1๋ถ€ํ„ฐ 3๊นŒ์ง€์˜ ์—ด ์„ ํƒํ•˜๊ธฐiris_data.iloc[10:21,1:4] ๐Ÿ‘‡ .iloc : ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์Šฌ๋ผ์ด์‹ฑํ•˜๊ธฐ  [Pandas] ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ (2) ๊ฒฐ์ธก์น˜ ํ™•์ธํ•˜..
240510 Today I Learn๐Ÿ’ก matplotlib์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ํ•˜๋‚˜๋กœ, ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋„๊ตฌ๋ฅผ ์ œ๊ณต2D ๊ทธ๋ž˜ํ”ฝ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์ฃผ๋กœ ์‚ฌ์šฉ์„  ๊ทธ๋ž˜ํ”„, ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„, ํžˆ์Šคํ† ๊ทธ๋žจ, ์‚ฐ์ ๋„, ํŒŒ์ด ์ฐจํŠธ ๋“ฑ ๋‹ค์–‘ํ•œ ์‹œ๊ฐํ™” ๋ฐฉ์‹์„ ์ง€์›๊ทธ๋ž˜ํ”„๋ฅผ ์ƒ‰์ƒ, ์Šคํƒ€์ผ, ๋ ˆ์ด๋ธ”, ์ถ• ๋ฒ”์œ„ ๋“ฑ์„ ์กฐ์ ˆํ•˜์—ฌ ์›ํ•˜๋Š” ํ˜•ํƒœ๋กœ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ์Œ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ ๋„๊ตฌmatplotlib๋กœ ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐimport pandas as pdimport matplotlib.pyplot as plt ๐Ÿ’ฝ ์˜ˆ์‹œ ๋ฐ์ดํ„ฐ์…‹๋”๋ณด๊ธฐnewjeans = pd.DataFrame({ 'name' : ['Minji','Hanni','Danielle','Haerin', 'Hyein'], 'age' : [20, ..
240509 Today I Learn๋ฐ์ดํ„ฐ ๋ณ‘ํ•ฉํ•˜๊ธฐ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ๋ถ™์ด๊ธฐ Concat๐Ÿ’ก Concat([๋ถ™์ผ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„], axis = ์ƒํ•˜(0)/์ขŒ์šฐ(1))pd.concat([df1, df2], axis = 0, ignore_index = False)axis์˜ ๊ธฐ๋ณธ๊ฐ’์€ 0(์œ„์•„๋ž˜๋กœ ๋ถ™์ด๊ธฐ)ignore_index์˜ ๊ธฐ๋ณธ๊ฐ’์€ False(์›๋ž˜ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์—์„œ์˜ ์ธ๋ฑ์Šค ์œ ์ง€)→ `ignore_index = True`๋‚˜ `.reset_index(drop=True)`๋ฅผ ํ™œ์šฉํ•ด ์ƒˆ๋กœ์šด ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ์Œaxis = 0 / axis = 1df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2'], 'B': ['B0', 'B1', 'B2']})df2 = pd.DataFrame({'A': [..
240509 Today I Learn๐Ÿ’ฝ ํ™œ์šฉ ๋ฐ์ดํ„ฐ์…‹ Titanic - Machine Learning from Disaster | Kaggle www.kaggle.com# data ๋ถˆ๋Ÿฌ์˜ค๊ธฐdf = pd.read_csv("train.csv")  ๋ฐ์ดํ„ฐ ํ™•์ธํ•˜๊ธฐ1. ๋ฐ์ดํ„ฐ์˜ ์ •๋ณด ํ™•์ธ : info()๐Ÿ’ก dataframe.info()df.info()๋”๋ณด๊ธฐ์ธ๋ฑ์Šค, ์ปฌ๋Ÿผ๋ช…, ์ปฌ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜, ๋ฐ์ดํ„ฐ ํƒ€์ž…๋“ฑ ๋ฐ์ดํ„ฐ์˜ ์ •๋ณด๋ฅผ ํ™•์ธnull๊ฐ’ ํ™•์ธํ•  ๋•Œ๋„ ์‚ฌ์šฉํ•œ๋‹ค.์ƒ์œ„ 5๊ฐœ ๊ฐ’๋งŒ ์ถ”์ถœํ•˜๊ธฐ : head()๐Ÿ’ก dataframe.head(n)df.head()โ€‹๋”๋ณด๊ธฐdataframe์—์„œ ์ƒ์œ„ n๊ฐœ์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.๊ณต๋ฐฑ์ผ ๊ฒฝ์šฐ ์ƒ์œ„ 5๊ฐœ์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.๋ฐ์ดํ„ฐ์˜ ๊ธฐ์ดˆํ†ต๊ณ„๋Ÿ‰ ํ™•์ธํ•˜๊ธฐ : describe()๐Ÿ’ก dataf..
240509 Today I Learn๐Ÿ’ก  ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ์‹ค์ œ ๋ฐ์ดํ„ฐ๋Š” ์›ํ•˜๋Š” ํ˜•ํƒœ๋กœ ๊ตฌ์ถ•๋˜์–ด์žˆ์ง€ ์•Š๊ธฐ ๋–„๋ฌธ์— ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•ด์ค˜์•ผ ํ•จ. ์ด ๋ชจ๋“  ํ™œ๋™์„ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๋ผ๊ณ  ํ•œ๋‹ค.๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ๋ฌด์—‡์„ ํ™•์ธํ•  ๊ฒƒ์ธ์ง€?์–ด๋–ค ์˜์‚ฌ๊ฒฐ์ •์„ ์œ„ํ•ด ํ•„์š”ํ•œ์ง€?๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ๋ฌด์—‡์„ ์–ป๊ณ ์ž ํ•˜๋Š”์ง€?์‹œ๋ฆฌ์ฆˆ vs. ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„DataFrame = ํ‘œ ํ˜•ํƒœindex : ๊ฐ ์•„์ดํ…œ์„ ํŠน์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณ ์œ ์˜ ๊ฐ’ (์—‘์…€์—์„œ๋Š” ์ขŒ์ธก ์—ด์ˆœ์„œ๋กœ ์ƒ๊ฐํ•˜๋ฉด๋จ)columns : ํ•˜๋‚˜์˜ ์†์„ฑ์„ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉSeries = ํ•˜๋‚˜์˜ ์†์„ฑ์„ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ (= DataFrame ํ‘œ์—์„œ ์—ด 1์ค„์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์‰ฌ์›€)value + index๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ / ์ €์žฅํ•˜๊ธฐ๐Ÿ’ก ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ / ์ €์žฅํ•˜๊ธฐ# ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐd..
240508 Today I Learn๋ฆฌ์ŠคํŠธ๋ฆฌ์ŠคํŠธ์— ์š”์†Œ ์‚ฝ์ž…  : insert()๐Ÿ’ก Insert  : ๋ฆฌ์ŠคํŠธ์˜ ํŠน์ • ์œ„์น˜์— ํŠน์ • ๊ฐ’์„ ์‚ฝ์ž…ํ•˜๋Š” ํ•จ์ˆ˜insert(a, b)→ ๋ฆฌ์ŠคํŠธ์˜ a๋ฒˆ์งธ ์œ„์น˜์— b๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.  ์˜ˆ์‹œ ) movie_rank ๋ฆฌ์ŠคํŠธ์—์„œ "์Šˆํผ๋งจ"์„ "๋‹ฅํ„ฐ ์ŠคํŠธ๋ ˆ์ธ์ง€"์™€ "์Šคํ”Œ๋ฆฟ" ์‚ฌ์ด์— ์ถ”๊ฐ€ํ•˜๋ผ.movie_rank = ["๋‹ฅํ„ฐ ์ŠคํŠธ๋ ˆ์ธ์ง€", "์Šคํ”Œ๋ฆฟ", "๋Ÿญํ‚ค"]๋”๋ณด๊ธฐmovie_rank.insert(1, '์Šˆํผ๋งจ')print(movie_rank)## '๋‹ฅํ„ฐ ์ŠคํŠธ๋ ˆ์ธ์ง€', '์Šˆํผ๋งจ', '์Šคํ”Œ๋ฆฟ', '๋Ÿญํ‚ค', '๋ฐฐํŠธ๋งจ'๋ฆฌ์ŠคํŠธ์—์„œ ์š”์†Œ ์‚ญ์ œํ•˜๊ธฐ : del๐Ÿ’ก del : ๋ฆฌ์ŠคํŠธ์˜ ํŠน์ • ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ํ•จ์ˆ˜del ์‚ญ์ œํ•  ๊ฐ’โ€‹์˜ˆ์‹œ ) movie_rank ๋ฆฌ์ŠคํŠธ์—์„œ '๋Ÿญํ‚ค'๋ฅผ ์‚ญ์ œํ•˜๋ผ.mo..
ny:D
'๐Ÿ“’ Today I Learn/๐Ÿ Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (6 Page)