Python正在计算每个定制的TimeSeriesIndexedData的统计信息

2024-09-29 23:25:27 发布

您现在位置:Python中文网/ 问答频道 /正文

    UsageDate               CustID1  CustID2   .... CustIDn
0   2018-01-01 00:00:00     1.095
1   2018-01-01 01:00:00     1.129
2   2018-01-01 02:00:00     1.165
3   2018-01-01 04:00:00     1.697
. 
.
m   2018-31-01 23:00:00     1.835                     (m,n)

数据帧(df)有m行和n列。m是一个小时时间序列指数,从每月的第一个小时开始到每月的最后一个小时。 栏目是近10万的客户。 数据帧的每个单元的值都是能量消耗值

对于每个客户,我需要计算: 1) 平均每小时使用量-所以基本上是一个月内每天第一小时的平均值,一个月内每天第二小时的平均值等等

2)各客户使用量汇总

3)前3个使用小时-对于客户x,可以是“2018-01-01 01:00:00”, “2018-11-01 05:00:00”“2018-21-01 17:00:00”

4)倒数第3个使用小时-与上述解释类似

5)每个客户当月的平均使用量

我的主要问题是如何将每个客户的数据和一天中的某个小时或一天中的某个时间进行聚合

为了总结每个客户的使用情况,我尝试了: df_temp = pd.DataFrame(columns=["TotalUsage"])

for col in df.columns:

`df_temp[col,"TotalUsage"] = df[col].apply.sum()`

然而,这个和我尝试过的许多版本并没有帮助我解决这个问题

请帮助我找到解决这些问题的方法和方法

另外,由于数据帧很大,如果我们可以讨论计算复杂性以及如何减少计算时间,这将是很有帮助的


Tags: columns数据方法df客户时间coltemp
2条回答

这看起来像是pandas.groupby的工作

(我没有测试代码,因为我没有一个好的样本数据集来工作。如果有错误,请告诉我。)

对于某些要求,您需要添加一个包含小时的列:

 df['hour']=df['UsageDate'].dt.hour

1)按小时平均

 mean_by_hour=df.groupby('hour').mean()

2)按用户求和

 sum_by_uers=df.sum()

3)客户最高使用量。倒数第三个使用小时-与上面的解释类似。我不太明白你想要的结果,你可能在这个问题上问了太多不同的问题。如果您想要的是小时而不是值,我认为您可能需要遍历列。添加一个示例可能会有所帮助

4)相同意见

5)指顾客

mean_by_cust = df.mean()

我不确定这是否是您要查找的所有信息,但它将为您指明正确的方向:

import pandas as pd
import numpy as np

# sample data for 3 days
np.random.seed(1)
data = pd.DataFrame(pd.date_range('2018-01-01', periods= 72, freq='H'), columns=['UsageDate'])
data2  = pd.DataFrame(np.random.rand(72,5), columns=[f'ID_{i}' for i in range(5)])
df = data.join([data2])
# print('Sample Data:')
# print(df.head())
# print()

# mean of every month and hour per year
# groupby year month hour then find the mean of every hour in a given year and month
mean_data = df.groupby([df['UsageDate'].dt.year, df['UsageDate'].dt.month, df['UsageDate'].dt.hour]).mean()
mean_data.index.names = ['UsageDate_year', 'UsageDate_month', 'UsageDate_hour']
# print('Mean Data:')
# print(mean_data.head())
# print()

# use set_index with max and head
top_3_Usage_hours = df.set_index('UsageDate').max(1).sort_values(ascending=False).head(3)
# print('Top 3:')
# print(top_3_Usage_hours)
# print()

# use set_index with min and tail
bottom_3_Usage_hours = df.set_index('UsageDate').min(1).sort_values(ascending=False).tail(3)
# print('Bottom 3:')
# print(bottom_3_Usage_hours)

输出:

Sample Data:
            UsageDate      ID_0      ID_1      ID_2      ID_3      ID_4
0 2018-01-01 00:00:00  0.417022  0.720324  0.000114  0.302333  0.146756
1 2018-01-01 01:00:00  0.092339  0.186260  0.345561  0.396767  0.538817
2 2018-01-01 02:00:00  0.419195  0.685220  0.204452  0.878117  0.027388
3 2018-01-01 03:00:00  0.670468  0.417305  0.558690  0.140387  0.198101
4 2018-01-01 04:00:00  0.800745  0.968262  0.313424  0.692323  0.876389

Mean Data:
                                                   ID_0      ID_1      ID_2  \
UsageDate_year UsageDate_month UsageDate_hour                                 
2018           1               0               0.250716  0.546475  0.202093   
                               1               0.414400  0.264330  0.535928   
                               2               0.335119  0.877191  0.380688   
                               3               0.577429  0.599707  0.524876   
                               4               0.702336  0.654344  0.376141   

                                                   ID_3      ID_4  
UsageDate_year UsageDate_month UsageDate_hour                      
2018           1               0               0.244185  0.598238  
                               1               0.400003  0.578867  
                               2               0.623516  0.477579  
                               3               0.429835  0.510685  
                               4               0.503908  0.595140  

Top 3:
UsageDate
2018-01-01 21:00:00    0.997323
2018-01-03 23:00:00    0.990472
2018-01-01 08:00:00    0.988861
dtype: float64

Bottom 3:
UsageDate
2018-01-01 19:00:00    0.002870
2018-01-03 02:00:00    0.000402
2018-01-01 00:00:00    0.000114
dtype: float64

对于top和bottom 3,如果要查找跨行的最小和,则:

df.set_index('UsageDate').sum(1).sort_values(ascending=False).tail(3)

相关问题 更多 >

    热门问题