简体   繁体   English

使用汇总函数计数的Dataframe上的Pandas Timegrouper

[英]Pandas Timegrouper on Dataframe using aggregate function count

I'm working on Timegrouper on Data Frame from Excel and trying to do a Pviot using Date as column header and Time as row and aggregate count on Y is "Barton LLC". 我正在使用Excel中的数据框上的Timegrouper进行操作,尝试使用Date作为列标题和Time作为行以及Y上的总计数的“ Pviot”是“ Barton LLC”。

Data.xls 
X        Y               Z               D
740150  Barton LLC  B1-20000    2014-01-01 02:21:51
740150  Barton LLC  B1-50809    2014-01-01 02:21:51
740150  Barton LLC  B1-53102    2014-01-01 02:21:51
740150  Barton LLC  S2-16558    2014-01-02 21:21:01
740150  Barton LLC  B1-86481    2014-01-02 21:21:01
740150  Curlis L    S1-06532    2014-01-02 21:21:01
740150  Barton LLC  S1-47412    2014-01-02 21:21:01
740150  Barton LLC  B1-33364    2014-01-02 21:21:01
740150  Barton LLC  S1-93683    2014-02-07 04:34:50
740150  Barton LLC  S2-10342    2014-02-07 04:34:50

Tried using resample and pivot and timegrouper but got sequence of errors 使用重采样,数据透视和时间分组器进行了尝试,但出现了一系列错误

import pandas as pd
import numpy as np
df = pd.read_excel("data.xlsx")
ndf = df[df['Type'].eq('df')].pivot_table(columns= ['Y'],values='Y',
index=pd.Grouper(key='D',freq='H'),aggfunc='count',fill_value=0) 

Result 结果

         2014-01-01,2014-01-02,2014-02-07
 02:21    3,NaN,NaN              
 21:21    NaN,4,NaN
 04:34    NaN,NaN,2

You could split the datetime column in date and time and use pivot_table : 您可以按datetime拆分datetime列,并使用pivot_table

df['date'] = df['D'].dt.date
df['time'] = df['D'].dt.time
pd.pivot_table(df, 'D', 'time', 'date', aggfunc='count')

date       2014-01-01  2014-01-02  2014-02-07
time                                        
02:21:51         3.0         NaN         NaN
04:34:50         NaN         NaN         2.0
21:21:01         NaN         5.0         NaN

Note that you were missing one count for the date 2014-01-02 21:21:01 请注意,您在日期2014-01-02 21:21:01缺少一项

Use crosstab with strftime for convert datetime s to custom strings: 使用带有strftime crosstabdatetime转换为自定义字符串:

df.D = pd.to_datetime(df.D)

ndf = pd.crosstab(df['D'].dt.strftime('%H:%M').rename('H'), df['D'].dt.strftime('%Y-%m-%d')) 
print (ndf)
D      2014-01-01  2014-01-02  2014-02-07
H                                        
02:21           3           0           0
04:34           0           0           2
21:21           0           5           0

ndf = pd.crosstab(df['D'].dt.time.rename('T'), df['D'].dt.date) 
print (ndf)
D         2014-01-01  2014-01-02  2014-02-07
T                                           
02:21:51           3           0           0
04:34:50           0           0           2
21:21:01           0           5           0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM