๋ฐ์ดํ„ฐ๋ถ„์„๊ฐ€ ๊ณผ์ •/Python

DAY44. Python Pandas (2)DataFrame, ๊ธฐ์ˆ ํ†ต๊ณ„

LEE_BOMB 2021. 11. 16. 17:14
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