본문 바로가기

MLOps 개발자 양성과정/python

[Day-12] 데이터 분석을 위한 패키지-2 (pandas)

<목차>

 

Pandas

구조적 데이터 표시와 처리에 강함

 

01) 구조적 데이터 생성하기

1) series를 활용한 데이터 생성

 

 ㆍ pandas 불러오기

* pandas 별칭 pd

import pandas as pd

 ㆍ Series 생성하기

: 새로축 라벨을 index, 입력한 시퀀스 데이터를 values라고 한다.

s1 = pd.Series([10,20,30,40,50])
s1
>>> 0    10
1    20
2    30
3    40
4    50
dtype: int64

ㆍ index와 values 분리해서 가져오기

s1.index
>>> RangeIndex(start=0, stop=5, step=1)

s1.values
>>> array([10, 20, 30, 40, 50], dtype=int64)

 

ㆍ 혼합된 리스트를 인자로 데이터 생성

※ Numpy 모든 원소의 데이터 타입이 같아햐 함

※ Pandas 데이터 타입 달라도 가능함

s2 = pd.Series(['a','b','c',1,2,3]) #단, object로 처리
s2
>>> 
0    a
1    b
2    c
3    1
4    2
5    3
dtype: object

ㆍ 데이터가 없음 표기 가능

Numpy를 import하여 np.nan 사용 -> 특정 원소가 없어도 가능

import numpy as np
s3 = pd.Series([np.nan,10,30]) #nan = not a number
s3
>>> 
0     NaN
1    10.0
2    30.0
dtype: float64

 

ㆍ 인자로 index 추가

s = pd.Series(seq_data, index = index_seq)
※ seq_data와 index_seq의 개수가 일치해야 함
index_date = ['2023-01-01','2023-01-02','2023-01-03','2023-01-04']
s4 =pd.Series([200,195,np.nan,205], index = index_date)
s4
>>> 2023-01-01    200.0
2023-01-02    195.0
2023-01-03      NaN
2023-01-04    205.0
dtype: float64

 

ㆍ 데이터와 index 함게 입력 =>딕셔너리 이용하기

s5 = pd.Series({'국어':100, '영어':95, '수학':90})
s5
>>> 국어    100
영어     95
수학     90
dtype: int64