2015-01-15 5 views
0

각각 datetime 인덱스가있는 두 개의 데이터 프레임이 있고 이름없는 데이터 열이 하나씩 있다고 가정합니다. 데이터 프레임의 길이가 다르며 datetime 인덱스가 겹치지 않을 수도 있습니다.상관 관계 분석을위한 Python 팬더 그룹화

df1은 길이 20입니다. df2는 길이 400입니다. 데이터 열은 임의의 수레로 구성됩니다.

반복 당 20 단위를 취하여 df2를 반복하면서 각 반복이 시작 벡터를 한 단위 씩 증가시키고 종료 벡터를 한 단위 씩 증가 시키려합니다. 각 반복에서 나는 df1의 20 단위와 df2의 반복을 위해 선택한 20 단위 사이의 상관 관계를 계산하려고합니다. 이 상관 계수 및 기타 통계가 기록됩니다.

일단 루프가 완료되면 내 통계 검색을 만족시키는 df2의 20 단위 벡터로 df1을 플롯해야합니다. 따라서 분석이 완료되면 벡터를 다시 획득하기 위해 일정 수준의 색인 생성을 유지해야합니다.

의견이 있으십니까?

답변

0

왜 그런지 또는 날짜를 중요하게 생각하는지,이 질문에 대한 자세한 내용을 알지 못하면서 질문 한대로 처리됩니다. 보내 주신 의견에 따라 업데이트 해 드리겠습니다.

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
import random 

df1 = pd.DataFrame({'a':[random.randint(0, 20) for x in range(20)]}, index = pd.date_range(start = '2013-01-01',periods = 20, freq = 'D')) 
df2 = pd.DataFrame({'b':[random.randint(0, 20) for x in range(400)]}, index = pd.date_range(start = '2013-01-10',periods = 400, freq = 'D')) 

corr = pd.DataFrame() 
for i in range(0,380): 

    t0 = df1.reset_index()['a'] # grab the numbers from df1 
    t1 = df2.iloc[i:i+20].reset_index()['b'] # grab 20 days, incrementing by one each time 
    t2 = df2.iloc[i:i+20].index[0] # set the index to be the first day of df2 

    corr = corr.append(pd.DataFrame({'corr':t0.corr(t1)}, index = [t2])) #calculate the correlation and append it to the DF 

# plot it and save the graph 
corr.plot() 
plt.title("Correlation Graph") 
plt.ylabel("(%)") 
plt.grid(True) 
plt.show() 
plt.savefig('corr.png')