본문 바로가기

포트폴리오/api

pandas Series,DataFrame 기본




아래 코드의 출처는 https://wikidocs.net/4372 입니다.

	
from pandas import Series, DataFrame

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

data = DataFrame(raw_data)
print(data)
	

DataFrame에서 raw를 확인활 때는 아래와 같이 ix[] 메서드를 사용해야 함.

	
day_data = daeshin_day.ix['16.02.24']
print(day_data)
print(type(day_data))
	
그 결과는
	
open     11100
high     11100
low      10950
close    11100
Name: 16.02.24, dtype: int64
		
	

DataReader 사용하기

	
In [1]: import pandas_datareader.data as web

In [2]: import datetime

In [3]: start = datetime.datetime(2016, 2, 19)

In [4]: end = datetime.datetime(2016, 3, 4)		
	
	
In [6]: gs
Out[6]: 
Open High Low Close Volume Adj Close
Date 
2016-02-19 50300 51100 49950 50600 301800 50600
2016-02-22 50300 50800 49850 50400 153900 50400
2016-02-23 50800 53000 50800 52800 431900 52800
2016-02-24 52000 53500 51900 53500 259600 53500
2016-02-25 53900 54700 52700 53900 170800 53900
2016-02-26 54300 54500 53200 53300 159400 53300
2016-02-29 53000 53900 52800 53000 154600 53000
2016-03-01 53000 53000 53000 53000      0 53000
2016-03-02 53900 55200 53500 55100 229900 55100
2016-03-03 55300 55800 54300 55000 217000 55000
2016-03-04 54700 54800 53200 54200 237900 54200
	

Chart 그리기

	
import matplotlib.pyplot as plt
plt.plot(gs['Adj Close'])
 
plt.show()
	

x축에 날짜가 표시되지 않을 때

	
In [17]: gs.index
Out[17]: 
DatetimeIndex(['2010-01-04', '2010-01-05', '2010-01-06', '2010-01-07',
'2010-01-08', '2010-01-11', '2010-01-12', '2010-01-13',
'2010-01-14', '2010-01-15',
...
'2016-02-22', '2016-02-23', '2016-02-24', '2016-02-25',
'2016-02-26', '2016-02-29', '2016-03-01', '2016-03-02',
'2016-03-03', '2016-03-04'],
dtype='datetime64[ns]', name='Date', length=1588, freq=None)
In [19]: plt.plot(gs.index, gs['Adj Close'])
Out[19]: []

In [20]: plt.show()