์คํฐ๋ ๋ ธํธ (EDA) Pandas ๊ธฐ์ด
๐ Pandas ๊ธฐ์ด
- python์์ R ๋งํผ์ ๊ฐ๋ ฅํ ๋ฐ์ดํฐ ํธ๋ค๋ง ์ฑ๋ฅ์ ์ ๊ณต
- ๋จ์ผ ํ๋ก์ธ์ค์์ ์ต๋์ ํจ์จ
- ์ฝ๋ฉ์ด ๊ฐ๋ฅํ๊ณ ์์ฉ๊ฐ๋ฅํ ์์ (๋ฐ์ดํฐ๋ฅผ ์ ์ ํ๋๋ฐ์ ๋ง์ด ์ฐ์ธ๋ค)
๐ Series
- index์ value๋ก ์ด๋ฃจ์ด์ ธ ์๋ค.
- ํ ๊ฐ์ง ๋ฐ์ดํฐ ํ์ ๋ง ๊ฐ์ง ์ ์๋ค (float, object, int ๋ฑ)
- ํ๋๋ผ๋ ๋ฌธ์์ด์ด ์์ฌ์์ผ๋ฉด "object" ๋ก ์ธ์ (int๋ก ์ฐ์ฐ ๋ถ๊ฐ)
๐ ๋ฐ์ดํฐ ์ฝ๊ธฐ
- import pandas
- ๊ธฐ๋ณธ์ ์ผ๋ก csv, ์์ ๋ฑ ๋ค์ํ ๋ฐ์ดํฐ๋ฅผ DataFrame ํํ๋ก import ํ ์ ์๋ค.
๐ฉColumn(ํ), Index(), Value๋ก ๋๋๋ค
๐ฉhead, tail ๋ช ๋ น์ด
- head() - ์์ 5๊ฐ ๋ฐ์ดํฐ ๊ฐ์ ๋ณด์ฌ์ค๋ค. (๊ฐ์๋ก ์ง์ ๊ฐ๋ฅ)
- tail() - ํ์ 5๊ฐ ๋ฐ์ดํฐ ๊ฐ์ ๋ณด์ฌ์ค๋ค.
๐ฉrename ๋ช ๋ น์ด
- rename์ผ๋ก ์ปฌ๋ผ์ ์ด๋ฆ์ ๋ฐ๊ฟ ์ ์๋ค.
- rename={xxx.columns[0] : "๊ตฌ๋ณ"}, inplace=True
- 0๋ฒ์งธ ์ปฌ๋ผ์ ์ด๋ฆ์ "๊ตฌ๋ณ"๋ก ๋ฐ๊ฟ์ค๋ค
- inplace - ์์ผ๋ก ํ์ ์ ์ฉ ์ฌ๋ถ๋ฅผ ์ ํ (์ผ์์ or ์๊ตฌ)
๐ฉ๋ ์ง ๋ฐ์ดํฐ (date datas)
- import Pandas
- date_range("๋ ์ง", period = (์ผ์๊ฐ์))
๐ฉSeries์ DataFrame ์ฐจ์ด
- pandas.Series() - data(value), index
- data = 1์ฐจ์์ ์ธ ๋ฐ์ดํฐ๋ง ๊ฐ๋ฅ.
- data์ index๋ ๊ธธ์ด๊ฐ ๋ง์์ผ ํ๋ค.
- pandas.DataFrame() - data(value), index, columns
- csv, excel ํ์ผ์ ๋ถ๋ฌ์ค๊ฑฐ๋, DataFrame์ ํตํด ์ง์ ๋ง๋ค ์ ์๋ค
- ๋์ ๋๋ฆฌ๋ฅผ ํ์ฉ, column๊ณผ data๋ฅผ ์ง์ ํ์ฌ ๋ง๋ค ์ ์๋ค
๐ฉindex, value, columns
- DataFrame์ ๊ฐ๊ฐ ์ ๋ณด๋ฅผ ์ ์ ์๋ค.
๐ฉinfo() ๋ช ๋ น์ด
- info() ๋ช ๋ น์ด๋ฅผ ํตํด ๊ธฐ๋ณธ ์ ๋ณด๋ฅผ ํ์ธ ๊ฐ๋ฅ
- ๋ฐ์ดํฐ๋ฅผ ์ ์ ํ๊ธฐ ์ , info ๋ช ๋ น์ด๋ฅผ ํตํด ํ๋ฒ ๋ ํ์ธํด์ฃผ๋ ์์ ์ด ํ์ํ๋ค.
๐ฉdescribe()
- ๋ฐ์ดํฐ ํ๋ ์์ ๊ธฐ์ ํต๊ณ ์ ๋ณด ํ์ธ
- min, 25%, 50%, 75%, max ๋ฑ ๋ค์ํ ๊ฐ์ ํ์ธํ ์ ์๋ค
๐ฉ๋ฐ์ดํฐ ์ ๋ ฌ sort_values(), sort_index()
- sort_values(by = "column", ascending=True or False, inplace=True)
- sort_index() -> index๋ฅผ ์ ๋ ฌ (๊ธฐ๋ณธ์ ์ผ๋ก ์ค๋ฆ์ฐจ์)
- ascending, inplace ์ ๋ ฅ ํ์ธ
- inplace=True ์ํ๋ฉด ๋ณ๊ฒฝํ๊ธฐ ์ ๋ฐ์ดํฐ๊ฐ ๋ ์ถ๋ ฅ๋ ์ ์๋ค. ๊ผญ ํ์ธํ์!
- by์ ๊ธฐ์ค์ผ๋ก ์ผ์ ์ปฌ๋ผ์ ์์ฑ
๐ฉ๋ฐ์ดํฐ ์ ํ ["columns"]
- ํน์ ์ปฌ๋ผ์ ์ ํํ์ฌ ์กฐํํ ์ ์๋ค.
- ๋ฆฌ์คํธ ํํ๋ฅผ ์ฌ์ฉํ๊ฑฐ๋, ๊ทธ๋ฅ ์ปฌ๋ผ์ ์ ๋ ฅํด๋ ์กฐํ๊ฐ ๊ฐ๋ฅํ๋ค
- ๋ ๊ฐ ์ด์์ ์ปฌ๋ผ์ ์ ํํ ๋, list ํํ๋ก๋ง ์์ฑ์ด ๊ฐ๋ฅ
๐ฉoffset index [n:m]
- [n:m] - n๋ถํฐ m-1 ๊น์ง index๋ฅผ ์ฌ๋ผ์ด์ฑ.
- ex) df[0:3] 0๋ถํฐ 2๊น์ง index๋ฅผ ํ์.
- ["index name":"index name2"] ์ผ๋ก๋ ์ฌ๋ผ์ด์ฑ์ด ๊ฐ๋ฅํ๋ค.
๐ฉloc [[index:index],["columns:columns"]]
- loc = location
- index ์ด๋ฆ์ผ๋ก ํน์ ํ, ์ด์ ์ ํ.
- " , " / " : " ์ ์ฐจ์ด๋ฅผ ์ ๋ ํด์ผํ๋ค.
- Series ํํ์ dataframe ํํ๊ฐ ๊ตฌ๋ถ์ด ๋ ์ ์๋ค.
๐ฉiloc [[index:index],["columns:columns"]]
- iloc : inter location
- ์ปดํจํฐ๊ฐ ์ธ์ํ๋ ์ธ๋ฑ์ค ๊ฐ์ผ๋ก ์ ํ
๐ฉloc์ iloc์ ์ฐจ์ด?
- index, column์ ์ด๋ฆ์ ๊ธฐ์ค์ผ๋ก ์ฐ๋๋, ์๋ฒ์ ๊ธฐ์ค์ผ๋ก ์ฐ๋๋
- ex) ์ด๋ฆ : [20230804], [A, B] / ์๋ฒ : [0:4], [1, 3] ๋ฑ
๐ฉcondition
- ์กฐ๊ฑด์ ๊ฑฐ๋ ๊ฒ ex) df["A"] > 0 ๋ฑ
- ํน์ ์ปฌ๋ผ์ ์กฐ๊ฑด์ ํ์ธํ์ฌ Series ๋๋ Dataframe ํํ๋ก ์ถ๋ ฅ
- NaN : Not a Number
๐ฉcolumn ์ถ๊ฐ
- ๊ธฐ์กด ์ปฌ๋ผ์ด ์์ผ๋ฉด, ์ถ๊ฐ
- ๊ธฐ์กด ์ปฌ๋ผ์ด ์์ผ๋ฉด, ์์
๐ฉisin()
- ํน์ ์์๊ฐ ์๋์ง ํ์ธํ ์ ์๋ค.
- Dataframe ํํ๋ก๋ ์ถ๋ ฅํ ์ ์๋ค.
๐ฉํน์ ์ปฌ๋ผ ๋ฐ ์ธ๋ฑ์ค ์ ๊ฑฐ del, drop
- del df_name["column"]
- ํน์ ์ปฌ๋ผ ์ ๊ฑฐ
- df_name.drop(["column"], axis = 1) / df_name.drop("index", axis = 0)
- ํน์ ์ปฌ๋ผ๊ณผ ์ธ๋ฑ์ค ๋๋ค ์ ๊ฑฐ
- axis = 0 ๊ฐ๋ก (์ธ๋ฑ์ค) / axis = 1 ์ธ๋ก (์ปฌ๋ผ)
๐ฉapply()
- Dataframe์ ์ผ๊ด์ ์ผ๋ก ํจ์์ ๊ธฐ๋ฅ์ ์ ์ฉ
- axis๋ฅผ ํ์ฉํด index, column ๊ธฐ์ค์ ์ ํ ์ ์๋ค
- apply("def")
- sum, mean, min, max ๋ฑ ๋ค์ํ ํจ์ ์ฌ์ฉ ๊ฐ๋ฅ