如何使用pandas中的日期时间索引重新采样数据系列

时间:2013-09-19 20:56:29

标签: python pandas

  

使用pandas

中的日期时间索引重新采样数据系列

我是python的新手,我正在研究pandas。我有一个GW2test.csv文件,其中包含日期,时间和其他列,每30分钟收集一次数据。我需要重新采样每日平均值的数据。 CVS看起来像:

  

Date        time     P    P3W   P3W1      P2W
04/18/12    15:00   0   1.334           1.006
04/18/12    15:30   0   1.336           1.003
04/18/12    16:00   0   1.323           0.985
04/18/12    16:30   0   1.316           0.977
04/18/12    17:00   0   1.312  1.231    0.97

P是降水并且不总是零,P3W具有一些非测量值。 我做的是:

`

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import pylab as pl

df = pd.read_csv('GW2test.csv', parse_dates=[['Date','time']])

f = pd.DataFrame(df, columns=[ 'Date_time','P','P3E','P1W1', 'P1W', 'P2W'])

f.describe()

df1 = df.set_index('Date_time')

Daily= df1.resample('D', how=np**.mean)

Sel = Daily.ix[0:,['P']]

Sel.plot()

Sel = Daily.ix[0:,['P3W1']]

Sel.plot()

`

到目前为止,我的情节显示X中的每日频率,但是Y中的值是错误的。降水应该高达140,它最多只能达到3.5(30分钟值),我的P3W值是正确的,但显示了一个不连续的线,尽管我有整个时期的测量值。他们看起来像这样

请帮忙!

1 个答案:

答案 0 :(得分:0)

为什么不将Datetime作为单独的列,然后在groupby上执行Date并使用np.mean汇总每个组?这将生成仅包含平均值的Date索引的结果。并且可以使用相同的方法按time进行分组,并获取各个日期的平均值,以便您可以轻松查看所有15:00观察值的平均值。例如。

df.groupby("Date").agg(np.mean) 

可以忽略time列的平均值,也可以省略该列。

相关问题