(Python을 이용한 Quant 투자) pandas - 기초

Posted by : at

Category : Python



pandas 기본 문법

from pandas import Series

data = [100, 200, 300, 400]
s = Series(data)

print(s)
print(type(s))
0    100
1    200
2    300
3    400
dtype: int64
<class 'pandas.core.series.Series'>

from pandas import Series 

date = ['2018-08-01', '2018-08-02', '2018-08-03', '2018-08-04', '2018-08-05'] 

xrp_close = [512, 508, 512, 507, 500] 
s = Series(xrp_close, index=date) 

print(s)

print(s[0])
print(s['2018-08-01'])
2018-08-01    512
2018-08-02    508
2018-08-03    512
2018-08-04    507
2018-08-05    500
dtype: int64
512
512

from pandas import Series 

date = ['2018-08-01', '2018-08-02', '2018-08-03', '2018-08-04', '2018-08-05'] 
xrp_close = [512, 508, 512, 507, 500] 
s = Series(xrp_close, index=date) 

print(s.index) 
print(s.values)
Index(['2018-08-01', '2018-08-02', '2018-08-03', '2018-08-04', '2018-08-05'], dtype='object')
[512 508 512 507 500]

from pandas import Series 

date = ['2018-08-01', '2018-08-02', '2018-08-03', '2018-08-04', '2018-08-05'] 
xrp_close = [512, 508, 512, 507, 500] 
s = Series(xrp_close, index=date) 

print(s['2018-08-01': '2018-08-03'])
print(s[0:2])
2018-08-01    512
2018-08-02    508
2018-08-03    512
dtype: int64
2018-08-01    512
2018-08-02    508
dtype: int64

from pandas import Series 
 
date = ['2018-08-01', '2018-08-02', '2018-08-03', '2018-08-04', '2018-08-05']
xrp_close = [512, 508, 512, 507, 500] 
s = Series(xrp_close, index=date) 

s['2018-08-06'] = 490 
print(s.drop('2018-08-01')) 
print(s)

연산

my_list = [100, 200, 300, 400]
# print (my_list / 10)    # error - 딕셔너리는 이런 연산이 불가능

# 아래처럼 불편하게 연산을 진행해야 한다
new_list = []
for val in my_list: 
    new_list.append(val/10)
from pandas import Series 
s = Series([100, 200, 300, 400])

# pandas로 간단하게 연산가능
print(s /10)

About Taehyung Kim

안녕하세요? 8년차 현업 C++ 개발자 김태형이라고 합니다. 😁 C/C++을 사랑하며 다양한 사람과의 협업을 즐깁니다. ☕ 꾸준한 자기개발을 미덕이라 생각하며 노력중이며, 제가 얻은 지식을 홈페이지에 정리 중입니다. 좀 더 상세한 제 이력서 혹은 Private 프로젝트 접근 권한을 원하신다면 메일주세요. 😎

Star
Useful Links