Python Panda TIME 系列重采样 [英] Python Panda TIme series re sampling

查看:50
本文介绍了Python Panda TIME 系列重采样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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 系列重采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆