Python Panda TIme series re sampling

时光总嘲笑我的痴心妄想 提交于 2019-12-02 17:46:14

问题


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

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

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.


回答1:


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])

Alternatively, consider groupby.



来源:https://stackoverflow.com/questions/29283618/python-panda-time-series-re-sampling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!