Python Panda TIME 系列重新采样

Python Panda TIme series re sampling(Python Panda TIME 系列重新采样)
本文介绍了Python Panda TIME 系列重新采样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在用 panda 编写脚本,但我无法提取我想要的正确输出.这是问题:

I am writing scripts in panda but i could not able to extract correct output that i want. here it is problem:

我可以从 CSV 文件中读取这些数据.在这里你可以找到表结构

i can read this data from CSV file. Here you can find table structure

http://postimg.org/image/ie0od7ejr/

我想要上表数据的这个输出

I want this output from above table data

Month     Demo1 Demo 2
June 2013 3     1    
July 2013 2     2

在 Demo1 和 Demo2 列中,我想计算以 u 开头的常规条目和条目.六月共有 3 个常规条目,其中 1 个条目以 u 开头.

in Demo1 and Demo2 column i want to count regular entry and entry which starts with u. for June there are total 3 regular entry while 1 entry starts with u.

到目前为止,我已经编写了这段代码.

so far i have written this code.

   import sqlite3
   from pylab import *       
   import numpy as np
   import matplotlib.pyplot as plt
   import matplotlib.dates as mdates
   import datetime as dt

   conn = sqlite3.connect('Demo2.sqlite')
   df = pd.read_sql("SELECT * FROM Data", conn)
   df['DateTime'] = df['DATE'].apply(lambda x: dt.date.fromtimestamp(x))

   df1 = df.set_index('DateTime', drop=False)

感谢您的帮助.最终结果将是条形图.我可以从上面提到的输出中绘制图表.

Thanks advace for help. End result would be bar graph. I can draw graph from output that i mention above.

推荐答案

对于resample,可以这样定义两个聚合函数:

For resample, you can define two aggregation functions like this:

def countU(x):
    return sum(i[0] == 'u' for i in x)

def countNotU(x):
    return sum(i[0] != 'u' for i in x)

print df.resample('M', how=[countU, countNotU])

或者,考虑 groupby.

这篇关于Python Panda TIME 系列重新采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)