
Pandas๋ ํฌ๊ฒ ๋ ๊ฐ์ง ํต์ฌ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋๋ค.
๋์ ๋๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ DataFrame์ ์์ฑํ๊ณ ์ฃผ์ ์์ฑ์ ํ์ธํฉ๋๋ค.
import pandas as pd
# DataFrame ์์ฑ
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Aritra'],
'Age': [25, 30, 35],
'Location': ['Seattle', 'New York', 'Kona']
}, index=['A', 'B', 'C']) # ์ธ๋ฑ์ค๋ฅผ ์ง์ ์ง์
print(df)
# ๊ฒฐ๊ณผ:
# Name Age Location
# A Alice 25 Seattle
# B Bob 30 New York
# C Aritra 35 Kona
# ์ฃผ์ ์์ฑ ํ์ธ
print(df.columns)
# Index(['Name', 'Age', 'Location'], dtype='object')
print(df.index)
# Index(['A', 'B', 'C'], dtype='object')
DataFrame์์ ํน์ ํ(Row)์ด๋ ์ด(Column)์ ์ ํํ๋ฉด Series๊ฐ ๋ฐํ๋ฉ๋๋ค.
# ์ฒซ ๋ฒ์งธ ํ(Row) ์ ํ (iloc ์ฌ์ฉ)
y = df.iloc[0]
print(type(y))
# <class 'pandas.core.series.Series'> -> Series ๊ฐ์ฒด์์ ํ์ธ
print(y)
# Name Alice
# Age 25
# Location Seattle
# Name: A, dtype: object
object ํ์
์ผ๋ก ํ์๋ฉ๋๋ค.df['Name'] (์ด๋ฆ์ผ๋ก ์ ๊ทผ)df.iloc[0] (์์๋ก ์ ๊ทผ) ๋๋ df.loc['A'] (๋ผ๋ฒจ๋ก ์ ๊ทผ)pd.Series(data, index, dtype, name, copy)
RangeIndex (0, 1, 2...)๊ฐ ์์ฑ๋จ.# ๊ธฐ๋ณธ ์์ฑ (์ธ๋ฑ์ค ์๋ ์์ฑ)
pd.Series([1, 2, 3, 4])
# ์ต์
์ง์ ์์ฑ
pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'], dtype='int32', name='foo')
๋์ ๋๋ฆฌ์ Key๊ฐ Series์ Index๊ฐ ๋ฉ๋๋ค.
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
pd.Series(d)
# a 1
# b 2
# ...
๋ง์ฝ ๋์
๋๋ฆฌ๋ก ์์ฑํ๋ฉด์ index๋ฅผ ๋ฐ๋ก ์ง์ ํ๋ฉด, ์ง์ ํ ์ธ๋ฑ์ค์ ํด๋นํ๋ ๊ฐ๋ง ๊ฐ์ ธ์ค๊ฑฐ๋ ์์ผ๋ฉด NaN(๊ฒฐ์ธก์น)์ผ๋ก ์ฑ์์ง๋๋ค.
copy=False (๊ธฐ๋ณธ๊ฐ): ์๋ณธ ๋ฐ์ดํฐ๊ฐ ๋ณ๊ฒฝ๋๋ฉด Series๋ ๋ณ๊ฒฝ๋ ์ ์์ (View).copy=True: ๋ณ๋์ ๋ฉ๋ชจ๋ฆฌ์ ๋ณต์ฌํ์ฌ ์๋ณธ๊ณผ ๋
๋ฆฝ์ ์.s.index = [...]๋ฅผ ํตํด ์ธ๋ฑ์ค๋ฅผ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.import numpy as np
import pandas as pd
# ์๋ณธ ๋ฐ์ดํฐ ๊ณต์ ์์
na = np.array([1, 2, 3])
s = pd.Series(na)
s.iloc[0] = 99
print(na) # [99 2 3] -> ์๋ณธ ๋ฐฐ์ด๋ ๋ณ๊ฒฝ๋จ!
# Copy ์ฌ์ฉ ์์
na = np.array([1, 2, 3])
s = pd.Series(na, copy=True)
s.iloc[0] = 100
print(na) # [1 2 3] -> ์๋ณธ ๋ฐฐ์ด ์ ์ง๋จ
Pandas๋ ๋ค์ํ ๋ฐฉ์์ ๋ฐ์ดํฐ ์ ํ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค.
| ์์ | ๋ฌธ๋ฒ ์์ | ๊ฒฐ๊ณผ ํ์ |
|---|---|---|
| Index(์์)๋ก ์ ํ | s.iloc[0] | ๊ฐ (Scalar) |
| Index๋ก ์ฌ๋ผ์ด์ฑ | s.iloc[5:10] | Series |
| Index ๋ฐฐ์ด๋ก ์ ํ | s.iloc[[2, 3]] | Series |
| Label(์ด๋ฆ)๋ก ์ ํ | s["a"] ๋๋ s.loc["a"] | ๊ฐ (Scalar) |
| Label๋ก ์ฌ๋ผ์ด์ฑ | s["b":"d"] | Series (๋ ํฌํจ) |
| ์กฐ๊ฑด(Boolean) ์ ํ | s[s > 0] | Series |
| Dict ์คํ์ผ ํ์ธ | "b" in s ๋๋ s.get("c") | Bool |
s.index = ['a', 'b', 'c']
print("b" in s) # True
print(s.get("c")) # 3
Pandas๋ NumPy์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฐ๋ณต๋ฌธ ์์ด ๋ฐ์ดํฐ๋ฅผ ์ผ๊ด ์ฒ๋ฆฌํ๋ ๋ฒกํฐํ ์ฐ์ฐ์ ์ง์ํฉ๋๋ค. ๋ํ, ์ฐ์ฐ ์ ์ธ๋ฑ์ค(Label)๋ฅผ ๊ธฐ์ค์ผ๋ก ์๋์ผ๋ก ์ ๋ ฌ(Alignment)ํ์ฌ ์ฐ์ฐ์ ์ํํฉ๋๋ค.
s + s ๋๋ s * 2์ ๊ฐ์ด ์์ฑํ๋ฉด Series์ ๋ชจ๋ ์์์ ๋ํด ์ฐ์ฐ์ด ์ํ๋ฉ๋๋ค.NaN(๊ฒฐ์ธก์น)์ด ๋ฉ๋๋ค.# s.iloc[1:] (๋ ๋ฒ์งธ๋ถํฐ ๋๊น์ง), s.iloc[:-1] (์ฒ์๋ถํฐ ๋ค์์ ๋ ๋ฒ์งธ๊น์ง)
s.iloc[1:] + s.iloc[:-1]
# ์ธ๋ฑ์ค๊ฐ ๊ฒน์น๋ ๋ถ๋ถ๋ง ๊ณ์ฐ๋๊ณ , ๊ฒน์น์ง ์๋ ๋ถ๋ถ์ NaN ์ฒ๋ฆฌ๋จ.
# a NaN
# b 4.0
# c NaN
# dtype: float64
ValueError๊ฐ ๋ฐ์ํฉ๋๋ค.index ํ๋ผ๋ฏธํฐ๋ก ํ ๋ ์ด๋ธ์ ์ง์ ํ ์ ์์ต๋๋ค.columns๋ฅผ ์ง์ ํ ๋ ๋ฐ์ดํฐ์ ์๋ ์ปฌ๋ผ๋ช
์ ๋ฃ์ผ๋ฉด ํด๋น ์ปฌ๋ผ์ NaN์ผ๋ก ์ฑ์์ง๋๋ค.d = {
"one": pd.Series([1.0, 2.0, 3.0], index=["a", "b", "c"]),
"two": pd.Series([1.0, 2.0, 3.0, 4.0], index=["a", "b", "c", "d"]),
}
# Series๋ก ๊ตฌ์ฑ๋ Dict๋ ์ธ๋ฑ์ค๊ฐ ๋ฌ๋ผ๋ ํฉ์งํฉ(Union)์ผ๋ก ์ธ๋ฑ์ค๊ฐ ์์ฑ๋๊ณ ,
# ๋น์ด์๋ ๊ณณ์ NaN์ผ๋ก ์ฑ์์ง.
pd.DataFrame(d)
# one two
# a 1.0 1.0
# b 2.0 2.0
# c 3.0 3.0
# d NaN 4.0
๋์ ๋๋ฆฌ์ ๋ฆฌ์คํธ๋ก ์์ฑํ ๊ฒฝ์ฐ, ๊ฐ ๋์ ๋๋ฆฌ๊ฐ ํ๋์ ํ(Row)์ด ๋ฉ๋๋ค.
data = [{"a": 1, "b": 2}, {"a": 5, "b": 10, "c": 20}]
pd.DataFrame(data)
# a b c
# 0 1 2 NaN
# 1 5 10 20.0
DataFrame์ ๋์ ๋๋ฆฌ์ฒ๋ผ ๋ค๋ฃฐ ์ ์์ต๋๋ค.
df['one'] (Series ๋ฐํ)df['three'] = df['one'] * 2 (์๋ก์ด ์ปฌ๋ผ ์์ฑ)del df['two'] ๋๋ df.pop('three')df.insert(1, 'new_col', value) (ํน์ ์์น์ ์ปฌ๋ผ ์ถ๊ฐ)| ์์ | ๋ฌธ๋ฒ | ๊ฒฐ๊ณผ ํ์ |
|---|---|---|
| ์ด ์ ํ | df[col] | Series |
| ํ ์ ํ (Label) | df.loc[label] | Series |
| ํ ์ ํ (Index) | df.iloc[loc] | Series |
| ํ ์ฌ๋ผ์ด์ฑ | df[5:10] ๋๋ df.iloc[5:10] | DataFrame |
| ์กฐ๊ฑด๋ถ ์ ํ | df[bool_vec] | DataFrame |
Pandas๋ ๋ค์ํ ํ์ผ ํฌ๋งท์ ์ง์ํฉ๋๋ค.
| ํฌ๋งท | Reader (์ฝ๊ธฐ) | Writer (์ฐ๊ธฐ) |
|---|---|---|
| CSV | read_csv | to_csv |
| JSON | read_json | to_json |
| Excel | read_excel | to_excel |
| SQL | read_sql | to_sql |
read_csv ์ฃผ์ ํ๋ผ๋ฏธํฐfilepath_or_buffer: ํ์ผ ๊ฒฝ๋กsep: ๊ตฌ๋ถ์ (๊ธฐ๋ณธ๊ฐ ,)header: ํค๋ ์์น (๊ธฐ๋ณธ๊ฐ 0, ์์ผ๋ฉด None)index_col: ์ธ๋ฑ์ค๋ก ์ฌ์ฉํ ์ปฌ๋ผusecols: ๋ถ๋ฌ์ฌ ์ปฌ๋ผ ์ง์ encoding: ์ธ์ฝ๋ฉ (์: 'utf-8', 'cp949')skiprows: ๊ฑด๋๋ธ ํ ๊ฐ์read_csv์ ์ธ์ฝ๋ฉ ๋ฌธ์ ํด๊ฒฐ๊ณต๊ณต๋ฐ์ดํฐ(์: ์์ธ์ด๋ฆฐ๋ฐ์ดํฐ๊ด์ฅ)์์ ๋ค์ด๋ก๋ํ CSV ํ์ผ์ ํ๊ธ ์ธ์ฝ๋ฉ ๋ฌธ์ ๋ก ์ธํด ๊ธฐ๋ณธ ์ค์ ์ผ๋ก ์ฝ์ ๋ ์ค๋ฅ๊ฐ ์์ฃผ ๋ฐ์ํฉ๋๋ค. ์ด๋ฅผ ํด๊ฒฐํ๋ ๊ณผ์ ์ ์ดํด๋ด ๋๋ค.
Pandas์ read_csv๋ ๊ธฐ๋ณธ์ ์ผ๋ก encoding='utf-8'์ ์ฌ์ฉํฉ๋๋ค. ํ์ง๋ง ๋ง์ ํ๊ตญ ๊ณต๊ณต๋ฐ์ดํฐ๋ cp949๋ euc-kr๋ก ์ธ์ฝ๋ฉ๋์ด ์์ด ์๋์ ๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค.
import pandas as pd
# ๊ธฐ๋ณธ ์ค์ ์ผ๋ก ๋ก๋ ์๋
bike = pd.read_csv('seoul_bike_2502.csv')
print(bike.head())
๋ฐ์ ์๋ฌ (Traceback):
Traceback (most recent call last):
...
File "pandas/_libs/parsers.pyx", line 2053, in pandas._libs.parsers.raise_parser_error
File "<frozen codecs>", line 325, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 1: invalid start byte
์์ธ: ํ์ผ์ cp949๋ก ์ ์ฅ๋์ด ์๋๋ฐ, Pandas๋ utf-8 ๋ฐฉ์์ผ๋ก ํด์ํ๋ ค๋ค ์คํจํจ.
encoding='cp949')encoding ํ๋ผ๋ฏธํฐ์ 'cp949' (๋๋ 'euc-kr')์ ์ง์ ํ์ฌ ํ์ผ์ ์ฌ๋ฐ๋ฅด๊ฒ ์ฝ์ด์ต๋๋ค.
import pandas as pd
# encoding='cp949' ์ถ๊ฐํ์ฌ ํ๊ธ ๊นจ์ง ๋ฐฉ์ง
bike = pd.read_csv('seoul_bike_2502.csv', encoding='cp949')
# ์์ 5๊ฐ ํ ์ถ๋ ฅ
print(bike.head())
์คํ ๊ฒฐ๊ณผ:
์์ ๊ฑฐ๋ฒํธ ๋์ฌ์ผ์ ๋์ฌ ๋์ฌ์๋ฒํธ ๋์ฌ ๋์ฌ์๋ช
... ์ด์ฉ์์ข
๋ฅ ๋์ฌ๋์ฌ์ID ๋ฐ๋ฉ๋์ฌ์ID ์์ ๊ฑฐ๊ตฌ๋ถ
0 SPB-41846 2025-02-01 00:01:04 01308 ์์๋กํฐ๋ฆฌ ๋ฒ์ค์ ๋ฅ์ฅ ์ ... ๋ด๊ตญ์ธ ST-827 ST-273 BIK_002
1 SPB-60204 2025-02-01 00:00:14 03500 ๊ตฐ์์ญ2๋ฒ์ถ๊ตฌ ... ๋ด๊ตญ์ธ ST-983 ST-1266 BIK_002
2 SPB-60407 2025-02-01 00:01:54 00398 ์์ง๋ก3๊ฐ์ญ 3๋ฒ์ถ๊ตฌ ... ๋ด๊ตญ์ธ ST-1435 ST-943 BIK_002
3 \\N 2025-02-01 00:01:34 00864 ์์ฒํฅ๋ํ๋ณ์(ํ๋จ์ค๊ฑฐ๋ฆฌ) ... ๋ด๊ตญ์ธ ST-2188 ST-2188 BIK_002
4 SPB-50025 2025-02-01 00:00:30 00558 ์ฑ๋๊ด์ง ๊ต์ก์ง์์ฒญ ์ ... ๋ด๊ตญ์ธ ST-359 ST-2340 BIK_002
[5 rows x 17 columns]
read_sql_table ์ฃผ์ ํ๋ผ๋ฏธํฐtable_name: ํ
์ด๋ธ ์ด๋ฆcon: SQLAlchemy ์ฐ๊ฒฐ ๊ฐ์ฒดindex_col: ์ธ๋ฑ์ค๋ก ์ค์ ํ ์ปฌ๋ผparse_dates: ๋ ์ง ํ์์ผ๋ก ํ์ฑํ ์ปฌ๋ผhead(n) / tail(n): ์์/ํ์ n๊ฐ์ ํ์ ๋ณด์ฌ์ค๋๋ค. (๊ธฐ๋ณธ๊ฐ 5)sample(n): ๋ฌด์์๋ก n๊ฐ์ ํ์ ์ถ์ถํฉ๋๋ค.describe(): ์์นํ ๋ฐ์ดํฐ์ ๊ธฐ์ ํต๊ณ๋(ํ๊ท , ํ์คํธ์ฐจ, 4๋ถ์์ ๋ฑ)์ ์์ฝํด์ ๋ณด์ฌ์ค๋๋ค.include='all': ๋ฌธ์์ด ๋ฐ์ดํฐ๊น์ง ํฌํจํ์ฌ ์์ฝ.import pandas as pd
# ์์ธ์ ๋ฐ๋ฆ์ด ๋์ฌ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
bike = pd.read_csv("seoul_bike_2502.csv", encoding='cp949', parse_dates=['๋์ฌ์ผ์', '๋ฐ๋ฉ์ผ์'], date_format='%Y-%m-%d %H:%M:%S')
#print(bike.head())
print(bike.describe())
์คํ ๊ฒฐ๊ณผ:
๋์ฌ์ผ์ ๋ฐ๋ฉ์ผ์ ์ด์ฉ์๊ฐ(๋ถ) ์ด์ฉ๊ฑฐ๋ฆฌ(M)
count 1629540 1629540 1.629540e+06 1.629540e+06
mean 2025-02-16 16:46:03.578277632 2025-02-16 17:05:14.998458368 1.844827e+01 1.916543e+03
min 2025-02-01 00:00:11 2025-02-01 00:03:54 0.000000e+00 0.000000e+00
25% 2025-02-10 17:14:23.750000128 2025-02-10 17:35:26 5.000000e+00 7.485000e+02
50% 2025-02-17 08:46:29.500000 2025-02-17 08:56:11 9.000000e+00 1.236465e+03
75% 2025-02-24 06:06:18.750000128 2025-02-24 06:18:17.249999872 2.000000e+01 2.170000e+03
max 2025-02-28 23:59:57 2025-03-01 02:01:43 1.097000e+03 8.861735e+04
std NaN NaN 2.465302e+01 2.299137e+03
๋ฐ์ดํฐ ๋ถ์์์ ๊ฐ์ฅ ์ค์ํ ์ ์ฒ๋ฆฌ ๊ณผ์ ์ค ํ๋์ ๋๋ค.
| ์์ | ํจ์ | ์ค๋ช | ์์ |
|---|---|---|---|
| ํ์ธ | isna(), notna() ,isnull(), notnull() | ๊ฒฐ์ธก์น ์ฌ๋ถ๋ฅผ Boolean์ผ๋ก ๋ฐํ (df.isnull()๊ณผ ๋์ผ) | df.isnull() |
| ์ ๊ฑฐ | dropna() | ๊ฒฐ์ธก์น๊ฐ ํฌํจ๋ ํ/์ด์ ์ญ์ | df.dropna(thresh=2) |
| ์ฑ์ฐ๊ธฐ | fillna(value) | ๊ฒฐ์ธก์น๋ฅผ ํน์ ๊ฐ์ผ๋ก ์ฑ์ | df.fillna(0) |
| ์๊ณ์ด | ffill(), bfill() | ์(Forward) ๋๋ ๋ค(Backward)์ ๊ฐ์ผ๋ก ์ฑ์ | df.ffill() |
"์ด๋ค ๊ฐ์ฒด(Object)๊ฐ ํ์ ๋์ ๋ณํ์ง ์๋ ๊ณ ์ ํ ๊ฐ(Hash Value)์ ๊ฐ์ง๊ณ ์๊ณ , ๋ค๋ฅธ ๊ฐ์ฒด์ ๋น๊ตํ ์ ์๋ค๋ฉด ๊ทธ ๊ฐ์ฒด๋ Hashableํ๋ค." ๋ผ๊ณ ํฉ๋๋ค.
์ฝ๊ฒ ๋งํด, "์ฃผ๋ฏผ๋ฑ๋ก๋ฒํธ๋ ์ง๋ฌธ์ฒ๋ผ ๋ณํ์ง ์๋ ๊ณ ์ ID๋ฅผ ๋ง๋ค ์ ์๋๊ฐ?" ์ ๋ฌธ์ ์ ๋๋ค.
ํ์ด์ฌ์์ ์ด๋ค ๊ฐ์ฒด๊ฐ Hashable ํ๋ ค๋ฉด ๋ค์ ๋ ๊ฐ์ง ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์์ด์ผ ํฉ๋๋ค.
__hash__(): ๊ฐ์ฒด์ ๊ณ ์ ํ ์ ์(Integer) ๊ฐ์ ๋ฐํํด์ผ ํฉ๋๋ค. ์ด ๊ฐ์ ๊ฐ์ฒด๊ฐ ์ด์์๋ ๋์ ๋ณํ๋ฉด ์ ๋ฉ๋๋ค.__eq__(): ๋ค๋ฅธ ๊ฐ์ฒด์ ๊ฐ์์ง ๋น๊ตํ ์ ์์ด์ผ ํฉ๋๋ค. (Equality)์ด๊ฒ์ด Hashable์ ๊ฐ๋ฅด๋ ๊ฐ์ฅ ํฐ ๊ธฐ์ค์ ๋๋ค.
| ๊ตฌ๋ถ | ์ค๋ช | ์์ (Python) | Hashable ์ฌ๋ถ |
|---|---|---|---|
| Immutable (๋ถ๋ณ) | ์์ฑ๋ ํ ๊ฐ์ ๋ฐ๊ฟ ์ ์๋ ๊ฐ์ฒด | int, float, str, bool, tuple | โ Yes (๊ฐ๋ฅ) |
| Mutable (๊ฐ๋ณ) | ์์ฑ๋ ํ ๊ฐ์ ๋ฐ๊ฟ ์ ์๋ ๊ฐ์ฒด | list, dict, set | โ No (๋ถ๊ฐ๋ฅ) |
์ ๊ฐ๋ณ ๊ฐ์ฒด(List ๋ฑ)๋ Hashable์ด ์๋๊น์?
๋ฆฌ์คํธ [1, 2]๊ฐ ์๋ค๊ณ ๊ฐ์ ํด ๋ด ์๋ค. ์ด ๋ฆฌ์คํธ์ ๋ด์ฉ๋ฌผ์ ์ธ์ ๋ [1, 2, 3]์ผ๋ก ๋ฐ๋ ์ ์์ต๋๋ค. ๋ด์ฉ์ด ๋ฐ๋๋ฉด '๊ณ ์ ํ ๊ฐ(Hash)'๋ ๋ฐ๋์ด์ผ ํฉ๋๋ค. "๊ฐ์ด ์์๋ก ๋ณํ๋ ๋ ์์ ๊ณ ์ ํ ์ฃผ๋ฏผ๋ฒํธ๋ฅผ ํ์ ์ง์ ์ ์๋ค"๋ผ๊ณ ์ดํดํ์๋ฉด ๋ฉ๋๋ค.
# ์ ์, ๋ฌธ์์ด์ ํด์ ๊ฐ๋ฅ
print(hash(123)) # ์: 123
print(hash("Python")) # ์: -6401083652872391083
# ํํ(Tuple)์ ๋ถ๋ณ์ด๋ฏ๋ก ํด์ ๊ฐ๋ฅ
my_tuple = (1, 2, 3)
print(hash(my_tuple)) # ํด์๊ฐ ์์ฑ ์ฑ๊ณต
# ๋ฆฌ์คํธ(List)๋ ๊ฐ๋ณ์ด๋ฏ๋ก ํด์ ๋ถ๊ฐ๋ฅ
my_list = [1, 2, 3]
try:
print(hash(my_list))
except TypeError as e:
print(e) # "unhashable type: 'list'" ์๋ฌ ๋ฐ์
ํํ์ ๋ถ๋ณ์ด์ง๋ง, ํํ ์์ ๊ฐ๋ณ ๊ฐ์ฒด(๋ฆฌ์คํธ ๋ฑ)๊ฐ ๋ค์ด์์ผ๋ฉด ๊ทธ ํํ์ Hashable ํ์ง ์์ต๋๋ค.
bad_tuple = (1, 2, [3, 4])
# ํํ ์์ฒด๋ ๋ถ๋ณ์ด์ง๋ง, ์์ ๋ฆฌ์คํธ [3, 4]๊ฐ ๋ณํ ์ ์์ -> unhashable
# hash(bad_tuple) -> Error!
๊ฐ์ ๋ด์ฉ์์ ์ด ๊ฐ๋ ์ด ์ค์ํ๊ฒ ๋ค๋ค์ง ์ด์ ๋ Pandas์ ๋์ ์๋ฆฌ ๋๋ฌธ์ ๋๋ค.
Pandas์ Index๋ Column์ ์ด๋ฆ์ ๋ฐ์ดํฐ๋ฅผ ๋น ๋ฅด๊ฒ ์ฐพ๊ธฐ ์ํ ์ด์ (Key) ์ญํ ์ ํฉ๋๋ค.
'Name', 2023, (2023, 'Jan') ๋ฑ['Name', 'Age'] (๋ฆฌ์คํธ ์์ฒด๋ฅผ ํ๋์ ์ธ๋ฑ์ค ์ด๋ฆ์ผ๋ก ์ธ ์ ์์)Pandas DataFrame์ด๋ Series๋ฅผ ์์ฑํ ๋ ๋์
๋๋ฆฌ๋ฅผ ๋ง์ด ์ฌ์ฉํฉ๋๋ค. ํ์ด์ฌ์ ๋์
๋๋ฆฌ Key๋ ๋ฐ๋์ Hashable์ด์ด์ผ ํฉ๋๋ค.
# ๊ฐ๋ฅ (String Key)
d = {"a": 1, "b": 2}
# ๋ถ๊ฐ๋ฅ (List Key) -> Error ๋ฐ์
# d = {[1, 2]: "value"}
str, int, tuple)๋ Hashable์ด๋ค.list, dict)๋ Not Hashable์ด๋ค.