Python Panda TIME 系列重新采样

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

问题描述

限时送ChatGPT账号..

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

上一篇:在python中为时间序列图添加水平限制线 下一篇:蟒蛇 pandas .日期对象按单独的列拆分.

相关文章

最新文章