Code Day's Night

ichikawayのブログ

pandasでデータを間引く(リサンプリング)

大量の時系列データをプロットする場合、プロット数が多くなりすぎてグラフが綺麗にでないことがある。 その場合は、データを間引くわけだが、単純にfor文で回して一定期間を処理するのも良いが、pandasを使えば1発でできる。

この間引くという機能はresampleというメソッドを使えば可能。

pandas.DataFrame.resample — pandas 0.19.2 documentation

df_orig = pd.read_csv(file, names=['date','val'],parse_dates=['date'],infer_datetime_format=True,index_col='date')  #CSVからデータをロード

df = df_orig.resample("D").max()   #1日単位の最大値を取得
df = df_orig.resample("H").sum()   #1時間単位の合計値を取得
df = df_orig.resample("10T").mean()   #10分単位の平均値を取得