2015-01-23 3 views
0

평균 시간, 평균 온도 및 습도를 특정 날짜와 시간으로 계산하는 프로그램을 작성하려고합니다.하지만 왜 '나노'값을 얻는 지 알 수 없습니다. .? 여기 내 코드는 어떤 아이디어입니까?Datetime 인덱싱을 사용하여 데이터 프레임 데이터 분석

import pandas as pd 
import numpy as np 

df = pd.DataFrame.from_csv('C:\Users\Joey\Desktop\Python\CSV\TM4CVC.csv',index_col = None) 

df2 = pd.DataFrame({'temp':df['Ch1_Value'], 
'press':df['Ch2_Value'], 
    'humid':df['Ch3_Value'], 'Date' : df['Date'], 'Time' : df['Time']}) 

df2['DateTime'] = pd.to_datetime(df2.apply(lambda x: x['Date']+ ' '+ x['Time'], 1)) 

df2.index = pd.to_datetime(df2.pop('DateTime')) 

df3 = df2.drop(['Date', 'Time'], 1) 

#------------------------------------------------------------------------------ 

def TempPressHumid(datetime_i, datetime_e): 

    index = df3[datetime_i:datetime_e] 

    out = {'temp_avg':np.mean(index['temp']), 
    'temp_std':np.std(index['temp']), 
    'press_avg':np.mean(index['press']), 
    'press_std':np.std(index['press']), 
    'humid_avg':np.mean(index['humid']), 
    'humid_std':np.std(index['humid'])} 
    print out 


TempPressHumid(datetime_i = '2012-06-25 08:27:19', datetime_e = '2012-01-25 10:59:33') 

내 출력은 다음과 같습니다

{'humid_std': nan, 'press_std': nan, 'humid_avg': nan, 'temp_avg': nan, 'temp_std': nan, 'press_avg': nan} 

인쇄 DF3 저를 제공합니다

     humid press temp 
DateTime         
2012-06-25 08:21:19 1004.0 21.2 26.0 
2012-06-25 08:22:19 1004.0 21.2 26.0 
2012-06-25 08:23:19 1004.1 21.3 26.0 
----------------------------------------- 

등 ...

+0

데이터 샘플을 제공해 주실 수 있습니까? 'print df3'는 어떤 것을 출력하고 있습니까? 그렇다면 그것을 보여 주시겠습니까? –

+0

인쇄 df3은 4 개 열로 데이터 프레임을 인쇄합니다 ... 시간, 습도, 온도, 누름 – Joey

+0

읽을 수있는 형식을 유지하려면 원래 질문에 해당 데이터를 추가해야합니다. 또한 'pandas' 태그를 질문에 추가하면 노출이 늘어나고 답변을받을 확률이 높아집니다. –

답변

0

이 같은 시도 할 수 :

a = pd.Series(np.random.random_sample(1000)) 
b = pd.Series(np.random.random_sample(1000)) 
c = pd.Series(np.random.random_sample(1000)) 

df = pd.DataFrame({"temp": a, "press": b, "humid": c}) 

i = pd.date_range('20120625', periods=1000, freq="h") 

df.index = pd.to_datetime(i) 
을이 때 데이터 프레임 df에서

지금

     humid  press  temp 
2012-06-25 00:00:00 0.910517 0.588777 ... 
2012-06-25 01:00:00 0.742219 0.501180 
2012-06-25 02:00:00 0.810515 0.172370 
2012-06-25 03:00:00 0.215735 0.046797 
2012-06-25 04:00:00 0.094144 0.822310 
2012-06-25 05:00:00 0.662934 0.629981 
2012-06-25 06:00:00 0.876086 0.586799 
... 

당신은 당신이 원하는 값의 사전을 볼 수 TempPressHumid('2012-06-25 08:00:00', '2012-07-25 10:00:00', df) 호출하는 경우의 원하는 날짜의 평균과 표준 편차가 그래서

def TempPressHumid(start, end, df): 
    values = {'temp_mean':np.mean(df['temp'][start:end]), 
       'temp_std':np.std(df['temp'][start:end]), 
       'press_mean':np.mean(df['press'][start:end]), 
       'press_std':np.std(df['press'][start:end]), 
       'humid_mean':np.mean(df['humid'][start:end]), 
       'humid_std':np.std(df['humid'][start:end]), 
       } 
    print(values) 
    return 

범위 계산하게 보이는 .

관련 문제