Python Panda TIME 系列重新采样

2022-01-11 00:00:00 python time-series resampling

问题描述

我正在用 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.

相关文章