DAY44. Python Pandas (2)DataFrame, ๊ธฐ์ ํต๊ณ
DataFrame
pandas 2์ฐจ์ ์๋ฃ๊ตฌ์กฐ
DB์ table๊ณผ ์ ์ฌ
์นผ๋ผ(์ด) ๋จ์ ์์ดํ ์๋ฃํ
DataFrame์นผ๋ผ : Series
๋ฐฉ๋ฒ1
import pandas as pd
๋ฐฉ๋ฒ2
from pandas import DataFrame
1. DataFrame ๊ฐ์ฒด ์์ฑ ๋ฐฉ๋ฒ
list ์นผ๋ผ ์์ฑ
name = ['hong', 'lee', 'kang', 'yoo']
age = [35, 44, 45, 25]
pay = [350, 450, 500, 250]
dict(์ฌ์ ํ) data์์ฑ
data = {'name':name, 'age':age, 'pay':pay}
dataframe ์์ฑ
frame = pd.DataFrame(data=data,
columns=['name', 'age', 'pay']) #dictํ์ผ๋ก dataframe ์์ฑํ๋ฉด ์์ ๋๋ค์ผ์๋
print(frame)
name age pay
0 hong 35 350
1 lee 44 450
2 kang 45 500
3 yoo 25 250
dataframe ์์ฑ ๋ฐฉ๋ฒ2
frame2 = DataFrame(data=data,
columns=['name', 'age', 'pay'])
print(frame2)
name age pay
0 hong 35 350
1 lee 44 450
2 kang 45 500
3 yoo 25 250
-> ๋ ์์ฑ๋ฐฉ๋ฒ์ ๊ฒฐ๊ณผ๋ ๋์ผ
DataFrame ์ ๋ณด ํ์ธ
type(frame) #pandas.core.frame.DataFrame
frame.info() #str(df)
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3 -> ํ์ ๊ฐ์
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 name 4 non-null object
1 age 4 non-null int64
2 pay 4 non-null int64
dtypes: int64(2), object(1)
memory usage: 224.0+ bytes
DF['column'] vs DF$column
pay = frame['pay'] #DF์์ ํน์ฒญ ์นผ๋ผ ์ถ์ถ
type(pay) #pandas.core.series.Series : Series ๊ฐ์ฒด
print('๊ธ์ฌ ํ๊ท =', pay.mean()) #๊ธ์ฌ ํ๊ท = 387.5
2. csv & ์นผ๋ผ ์ฐธ์กฐ
import os #file ๊ฒฝ๋ก ๋ณ๊ฒฝ/ํ์ธ
os.chdir(r'C:\ITWILL\4_Python-2\data')
emp = pd.read_csv('emp.csv', encoding='utf-8')
print(emp.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 No 5 non-null int64
1 Name 5 non-null object
2 Pay 5 non-null int64
dtypes: int64(2), object(1)
memory usage: 248.0+ bytes
None
print(emp)
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 No 5 non-null int64
1 Name 5 non-null object
2 Pay 5 non-null int64
dtypes: int64(2), object(1)
memory usage: 248.0+ bytes
None
1) ๋จ์ผ ์นผ๋ผ ๋ถ๋ฌ์ค๊ธฐ
emp.No #DF.column ๋ฐฉ์
emp['No'] #index ๋ฐฉ์
emp['Pay']
emp['Pay'].plot() #์ ๊ทธ๋ํ
2) ๋ณต์ ์นผ๋ผ
emp[['No', 'Pay']] #์ค์ฒฉ list
emp[['No', 'Pay']].plot
3. subset๋ง๋ค๊ธฐ : old DF -> new DF
1) ํน์ ์นผ๋ผ ์ ํ : ์นผ๋ผ ์๊ฐ ์ ์ ๊ฒฝ์ฐ
subset1 = emp[['Name', 'Pay']]
subset1
Name Pay
0 ํ๊ธธ๋ 150
1 ์ด์์ 450
2 ๊ฐ๊ฐ์ฐฌ 500
3 ์ ๊ด์ 350
4 ๊น์ ์ 400
2) ํน์ ํ ์ ์ธ
subset2 = emp.drop(1) #2๋ฒ์งธ ํ ์ ๊ฑฐ
subset2
0 101 ํ๊ธธ๋ 150
2 103 ๊ฐ๊ฐ์ฐฌ 500
3 104 ์ ๊ด์ 350
4 105 ๊น์ ์ 400
3) ์กฐ๊ฑด์์ผ๋ก ์ ํ : ํน์ ์นผ๋ผ ๊ธฐ์ค
ex. ๊ธ์ฌ๊ฐ 400์ด์ ๊ด์ธก์น ์ ํ(350๋ฏธ๋ง ์ ์ธ)
subset3 = emp[emp.Pay >= 400] #DF[์กฐ๊ฑด์]
subset3
No Name Pay
1 102 ์ด์์ 450
2 103 ๊ฐ๊ฐ์ฐฌ 500
4 105 ๊น์ ์ 400
-> ๊ธฐ์กด ์๋ณธ์ ์์ธ ๊ทธ๋๋ก ์ ์ง
4) columns ์ด์ฉ : ์นผ๋ผ ์๊ฐ ๋ง์ ๊ฒฝ์ฐ
iris = pd.read_csv('iris.csv')
iris.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Sepal.Length 150 non-null float64
1 Sepal.Width 150 non-null float64
2 Petal.Length 150 non-null float64
3 Petal.Width 150 non-null float64
4 Species 150 non-null object
dtypes: float64(4), object(1)
memory usage: 6.0+ KB
cols = list(iris.columns)
cols #['Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Species']
cols[:4] #x๋ณ์
cols[-1] #y๋ณ์
iris_x = iris[['Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Species']]
iris_x.head
iris_x = iris[cols[:4]]
iris_x.head() #๋์ผํ ๊ฒฐ๊ณผ. ๋ ์ ํธ๋๋ ๋ฐฉ๋ฒ
iris_y = iris[cols[-1]] #iris['Species']์ ๋์ผ
์ , ํน์๋ฌธ์, ๊ณต๋ฐฑ ํฌํจ ๋ณ์ ์ ํ
iris.Sepal.Length #DF.colum -> ์ฌ์ฉ ๋ถ๊ฐ
iris['Sepal.Length'] #Length: 150, dtype: float64
iris['Sepal.Length'].mean() #5.843333333333335
4) DataFrame ํ๋ ฌ ์ฐธ์กฐ : DF[row, col]
์ฝ๋ก (:)์ผ๋ก ํ๋ ฌ ์ฐธ์กฐ ๋ฐฉ์
ํ์1) DF.loc[row,col] : ๋ช
์นญ(label) ๊ธฐ๋ฐ
ํ์2) DF.iloc[row, col] : ์์น(integer) ๊ธฐ๋ฐ
1) loc ์์ฑ : ๋ช
์นญ ์์ธ ๊ธฐ๋ฐ - ๋ฌธ์ ์์ธ
ํ์) DF.loc[ํlabel, ์ดlabel]
์ซ์ ์์ธ -> ๋ช
์นญ ์์ธ ํด์
2) iloc ์์ฑ : ์์น ์์ธ ๊ธฐ๋ฐ - ์ซ์ ์์ธ
ํ์) DF.loc[ํinteger, ์ดinteger]
print(emp)
No Name Pay
0 101 ํ๊ธธ๋ 150
1 102 ์ด์์ 450
2 103 ๊ฐ๊ฐ์ฐฌ 500
3 104 ์ ๊ด์ 350
4 105 ๊น์ ์ 400
์ด ์ด๋ฆ : label (๋ ์ด๋ธ)
ํ ์ด๋ฆ : integer
1) loc ์์ฑ : ๋ช
์นญ ๊ธฐ๋ฐ
emp.loc[0, :] #1ํ ์ ์ฒด
emp.loc[0] #1ํ ์ ์ฒด(์ด ์๋ต)
emp.loc[0:3] #3๋ฒ์งธ(X) 3๋ฒ์ ๋ ์ด๋ธ(O) 1ํ~4ํ ์ ํ
emp.loc[0:3, ['No', 'Pay']] #๋ถ์ฐ์
emp.loc[0:3, 'No' : 'Pay'] #์ฐ์
2) iloc ์์ฑ : ์์น(์ ์=integer) ๊ธฐ๋ฐ
emp.iloc[0, :] #1ํ ์ ์ฒด
emp.iloc[0]
emp.iloc[0:3] #1ํ~3ํ ์ ํ
emp.iloc[0:3, ['No' : 'Pay']] #[SyntaxError: invalid syntax] ๋ฌธ์์ด ์ฌ์ฉ ๋ถ๊ฐ ๋ฐ๋์ intํ ์ด์ฉ
emp.iloc[0:4, [0,2]] #๋ถ์ฐ์
emp.iloc[0:4, 0:] #์ฐ์
3) example : box ์ ํ
emp.loc[[1,3], ['No', 'Pay']]
emp.iloc[[1,3], [0, 2]]
No Pay
1 102 450
3 104 350
-> ๊ฒฐ๊ณผ๋ ๋์ผ
Descriptive
1. ์์ฝํต๊ณ๋
2. ์๊ด๊ณ์
import pandas as pd
import os
os.chdir(r'C:\ITWILL\4_Python-2\data')
product = pd.read_csv('product.csv')
product.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 264 entries, 0 to 263
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 a 264 non-null int64 -> ๊ฒฐ์ธก์น ์์, ์ ์ํ ํต๊ณ๋
1 b 264 non-null int64
2 c 264 non-null int64
dtypes: int64(3)
memory usage: 6.3 KB
product.head()
product.tail()
1. ์์ฝํต๊ณ๋
product.describe() #summary(product)
ํ/์ด ํต๊ณ
product.shape #(264, 3)
product.sum(axis=0) #ํ์ถ : ๊ฐ์ ์ด ๋ชจ์ (์ด์ ํฉ)
product.sum(axis=1) #์ด์ถ : ๊ฐ์ ํ ๋ชจ์ (ํ์ ํฉ)
์ฐํฌ๋ : ๋ถ์ฐ, ํ์คํธ์ฐจ
product.var() #axis=0
product.std() #axis=0
DF['์นผ๋ผ๋ช
']
product['a'].sum() #773
๋น๋์
product['a'].value_counts()
3 126
4 64
2 37
1 30
5 7
์ ์ผ๊ฐ : ์ค๋ณต๋์ง ์๋ ๊ฐ
product['a'].unique() #array([3, 4, 2, 5, 1], dtype=int64)
2. ์๊ด๊ณ์
product.corr() #์๊ด๊ณ์ ์ ๋ฐฉํ๋ ฌ ๋ฐํ
a b c
a 1.000000 0.499209 0.467145
b 0.499209 1.000000 0.766853
c 0.467145 0.766853 1.000000
iris = pd.read_csv('iris.csv')
iris.info()
4๊ฐ ๋ณ์ ์ ํ (์ผ์ข
์ subset ์์ฑ ๋ฐฉ๋ฒ)
df = iris.iloc[:, :4]
df.shape #(150, 4)
df.corr()
Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length 1.000000 -0.117570 0.871754 0.817941
Sepal.Width -0.117570 1.000000 -0.428440 -0.366126
Petal.Length 0.871754 -0.428440 1.000000 0.962865 -> ์ฐ๊ด์ฑ์ด ์ ์ผ ๋์. ๋ค์ค๊ณต์ ์ฑ ๋ฌธ์ ๋ฐ์.
Petal.Width 0.817941 -0.366126 0.962865 1.000000