(빅데이터) 판다스 기초

시리즈

pandas에서 제공하는 1차원 배열과 같은 데이터 구조입니다. Python 사전 또는 목록으로 변환할 수 있습니다.

import pandas as pd

d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
s = pd.Series(data=d, index=('a', 'b', 'c'))
print(s)
# a    1
# b    2
# c    3
# dtype: int64

# 데이터를 딕셔너리로 쓰면서 인덱스가 맞지 않으면 NaN으로 입력된다.
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
s = pd.Series(data=d, index=('x', 'y', 'z'))
print(s)
# x   NaN
# y   NaN
# z   NaN
# dtype: float64

# 인덱스를 따로 지정하지 않으면 0부터 시작하는 정숫값 사용
l = (1, 2, 3, 4)
s = pd.Series(l)
print(s)
# 0    1
# 1    2
# 2    3
# 3    4
print(s(0)) # 1

s = pd.Series(l, index=('a', 'b', 'c', 'd'))
print(s)
# a    1
# b    2
# c    3
# d    4
# dtype: int64

d = {'a': (1, 2), 'b': (3, 4), 'c': (5, 6)}
s = pd.Series(d)
print(s)
# a    (1, 2)
# b    (3, 4)
# c    (5, 6)
# dtype: object

# Pandas의 Series는 인덱싱이 다른 경우에도 알아서 인덱싱이 같은 값끼리 연산을 수행
mine = pd.Series((10, 20, 30), index=('naver', 'sk', 'kt'))
yours = pd.Series((10, 30, 20), index=('kt', 'naver', 'sk'))

merge = mine + yours
print(merge)
# kt       40
# naver    40
# sk       40
# dtype: int64

mul = mine * yours
print(mul)
# kt       300
# naver    300
# sk       400
# dtype: int64

데이터 프레임

pandas에서 제공하는 2차원 데이터 구조입니다. 이것은 사전으로 쉽게 할 수 있습니다.

import pandas as pd

raw_data = {'col0': (1, 2, 3, 4),
                        'col1': (10, 20, 30, 40),
                        'col2': (100, 200, 300, 400)}

data = pd.DataFrame(raw_data)
print(data)
#    col0  col1  col2
# 0     1    10   100
# 1     2    20   200
# 2     3    30   300
# 3     4    40   400

print(data('col0'))
# 0    1
# 1    2
# 2    3
# 3    4
# Name: col0, dtype: int64

# 인덱싱을 통해 접근한 리스트는 Series이다
# 즉, DataFrame은 여러 개의 Series로 구성된 2차원 자료구조이다.
print(type(data('col0')))
# <class 'pandas.core.series.Series'>

# 인덱스를 정해주지 않으면 기본적으로 0으로 시작하는 정숫값이 들어간다.
daeshin = {'open':  (11650, 11100, 11200, 11100, 11000),
           'high':  (12100, 11800, 11200, 11100, 11150),
           'low' :  (11600, 11050, 10900, 10950, 10900),
           'close': (11900, 11600, 11000, 11100, 11050)}

daeshin_day = pd.DataFrame(daeshin)
print(daeshin_day)
#    close   high    low   open
# 0  11900  12100  11600  11650
# 1  11600  11800  11050  11100
# 2  11000  11200  10900  11200
# 3  11100  11100  10950  11100
# 4  11050  11150  10900  11000

# DataFrame 객체 생성 시 columns 파라미터를 통해 컬럼 순서를 지정할 수 있다.
daeshin_day = pd.DataFrame(daeshin, columns=('open', 'close', 'high', 'low'))
print(daeshin_day)
#     open  close   high    low
# 0  11650  11900  12100  11600
# 1  11100  11600  11800  11050
# 2  11200  11000  11200  10900
# 3  11100  11100  11100  10950
# 4  11000  11050  11150  10900

# DataFrame 객체 생성 시 index 파라미터를 통해 인덱싱에 사용할 값을 지정할 수 있다.
date = ('16.02.29', '16.02.26', '16.02.25', '16.02.24', '16.02.23')
daeshin_day = pd.DataFrame(daeshin, columns=('open', 'close', 'high', 'low'),
                                                            index=date)
print(daeshin_day)
#            open  close   high    low
# 16.02.29  11650  11900  12100  11600
# 16.02.26  11100  11600  11800  11050
# 16.02.25  11200  11000  11200  10900
# 16.02.24  11100  11100  11100  10950
# 16.02.23  11000  11050  11150  10900

# DataFrame의 column에 접근할 때는 칼럼 이름으로 접근한다.
day_col = daeshin_day('open')
print(day_col)
print(type(day_col))
# 16.02.29    11650
# 16.02.26    11100
# 16.02.25    11200
# 16.02.24    11100
# 16.02.23    11000
# Name: open, dtype: int64
# <class 'pandas.core.series.Series'>

# DataFrame의 row에 접근할 때는 DataFrame.loc(key) 로 접근한다.
day_data = daeshin_day**.loc**('16.02.24')
print(day_data)
print(type(day_data))
# open     11100
# close    11100
# high     11100
# low      10950
# Name: 16.02.24, dtype: int64
# <class 'pandas.core.series.Series'>