Python Panda TIme series re sampling

2019-03-04 13:38发布

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条回答
ゆ 、 Hurt°
2楼-- · 2019-03-04 14:30

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.

查看更多
登录 后发表回答