为了账号安全,请及时绑定邮箱和手机立即绑定

为什么使用 group by 会使一些 id 消失

为什么使用 group by 会使一些 id 消失

守着星空守着你 2022-11-01 14:49:44
我在一个机器学习项目中工作,当我提取特征时,我发现一些消费者 LCLid 在我按 LCLid 分组时从数据集中消失了数据集:伦敦家庭 SmartMeter 能源消耗数据这是原始数据集这是我用来提取一些特征的代码LCLid=[]for i in range(68):    LCLid.append('MAC0000'+str(228+i))      consommation=data.groupby('LCLid')['KWH/hh'].sum()    consommation_min=data.groupby('LCLid')['KWH/hh'].min()    consommation_max=data.groupby('LCLid')['KWH/hh'].max()    consommation_mean=data.groupby('LCLid')['KWH/hh'].mean()    consommation_evening=data.groupby(['LCLid','period'])['KWH/hh'].mean()#creation de dataframelist_of_tuples = list(zip (LCLid, consommation, consommation_min, consommation_max, consommation_mean)) data2= pd.DataFrame(list_of_tuples, columns = ['LCLid', 'Consumption', 'Consumption_min', 'Consumption_max', 'Consumption_mean']) 正如您在执行代码后看到的那样,数据集在 LCLid 282 中停止,而在原始数据集中,数据集还包含从 283 到 295 的 LCLid
查看完整描述

1 回答

?
MYYA

TA贡献1868条经验 获得超4个赞

使用来自伦敦家庭 SmartMeter 能源消耗数据的低碳伦敦数据

问题是LCLid不会1从MAC000228到均匀递增MAC000295。

print(data.LCLid.unique())


array(['MAC000228', 'MAC000229', 'MAC000230', 'MAC000231', 'MAC000232',

       'MAC000233', 'MAC000234', 'MAC000235', 'MAC000237', 'MAC000238',

       'MAC000239', 'MAC000240', 'MAC000241', 'MAC000242', 'MAC000243',

       'MAC000244', 'MAC000245', 'MAC000246', 'MAC000248', 'MAC000249',

       'MAC000250', 'MAC000251', 'MAC000252', 'MAC000253', 'MAC000254',

       'MAC000255', 'MAC000256', 'MAC000258', 'MAC000260', 'MAC000262',

       'MAC000263', 'MAC000264', 'MAC000267', 'MAC000268', 'MAC000269',

       'MAC000270', 'MAC000271', 'MAC000272', 'MAC000273', 'MAC000274',

       'MAC000275', 'MAC000276', 'MAC000277', 'MAC000279', 'MAC000280',

       'MAC000281', 'MAC000282', 'MAC000283', 'MAC000284', 'MAC000285',

       'MAC000287', 'MAC000289', 'MAC000291', 'MAC000294', 'MAC000295'],

      dtype=object)


print(len(data.LCLid.unique()))


>>> 55

解决问题

import pandas as pd

import numpy as np


df = pd.read_csv('Power-Networks-LCL-June2015(withAcornGps)v2.csv')


# determine the rows needed for the MAC000228 - MAC000295

df[df.LCLid == 'MAC000228'].iloc[0, :]  # first row of 228

df[df.LCLid == 'MAC000295'].iloc[-1, :]  # last row of 295


# create a dataframe with the desired data

data = df[['LCLid', 'DateTime', 'KWH/hh (per half hour) ']].iloc[6989700:9032044, :].copy()


# fix the data

data.DateTime = pd.to_datetime(data.DateTime)

data.rename(columns={'KWH/hh (per half hour) ': 'KWH/hh'}, inplace=True)

data['KWH/hh'] = data['KWH/hh'].str.replace('Null', 'NaN')

data['KWH/hh'].fillna(np.nan, inplace=True)

data['KWH/hh'] = data['KWH/hh'].astype('float')

data.reset_index(drop=True, inplace=True)


# aggregate your functions

agg_data = data.groupby('LCLid')['KWH/hh'].agg(['sum', 'min', 'max', 'mean']).reset_index()



print(agg_data)

agg_data

        LCLid           sum    min    max      mean

0   MAC000228   5761.288000  0.021  1.616  0.146356

1   MAC000229   6584.866999  0.008  3.294  0.167456

2   MAC000230   8911.154000  0.029  2.750  0.226384

3   MAC000231   3174.314000  0.000  1.437  0.080663

4   MAC000232   2083.042000  0.005  0.736  0.052946

5   MAC000233   2241.591000  0.000  3.137  0.056993

6   MAC000234   9700.328001  0.029  2.793  0.246646

7   MAC000235   8473.999003  0.011  3.632  0.223194

8   MAC000237  22263.294998  0.036  4.450  0.598299

9   MAC000238   7814.889998  0.016  2.835  0.198781

10  MAC000239   6113.029000  0.015  1.346  0.155481

11  MAC000240   7280.662000  0.000  3.146  0.222399

12  MAC000241   4181.169999  0.024  1.733  0.194963

13  MAC000242   1654.336000  0.000  1.481  0.042088

14  MAC000243  11057.366999  0.009  3.588  0.281989

15  MAC000244   5894.271000  0.005  1.884  0.149939

16  MAC000245  22788.699005  0.037  4.743  0.580087

17  MAC000246  13787.060005  0.014  3.516  0.351075

18  MAC000248  10192.239001  0.000  4.351  0.259536

19  MAC000249  24401.468995  0.148  5.242  0.893042

20  MAC000250   5850.003000  0.000  2.185  0.148999

21  MAC000251   8400.234000  0.035  3.505  0.213931

22  MAC000252  21748.489004  0.135  4.171  0.554978

23  MAC000253   9739.408999  0.009  1.714  0.248201

24  MAC000254   9351.614001  0.009  2.484  0.238209

25  MAC000255  14142.974002  0.097  3.305  0.360220

26  MAC000256  20398.665001  0.049  3.019  0.520680

27  MAC000258   6646.485998  0.017  2.319  0.169666

28  MAC000260   5952.563001  0.006  2.192  0.151952

29  MAC000262  13909.603999  0.000  2.878  0.355181

30  MAC000263   3753.997000  0.015  1.060  0.095863

31  MAC000264   7022.967000  0.020  0.910  0.179432

32  MAC000267   8797.094000  0.029  2.198  0.224898

33  MAC000268   3734.252001  0.000  1.599  0.095359

34  MAC000269   2395.232000  0.000  1.029  0.061167

35  MAC000270  15569.711002  0.131  2.249  0.397501

36  MAC000271   7244.860000  0.028  1.794  0.184974

37  MAC000272   8703.658998  0.034  3.295  0.222446

38  MAC000273   3622.199002  0.005  5.832  0.092587

39  MAC000274  28724.718997  0.032  3.927  0.734422

40  MAC000275   5564.004999  0.012  1.840  0.161290

41  MAC000276  11060.774001  0.000  1.709  0.315724

42  MAC000277   8446.528999  0.027  1.938  0.241075

43  MAC000279   3444.160999  0.016  1.846  0.098354

44  MAC000280  12595.780001  0.125  1.988  0.360436

45  MAC000281   6282.568000  0.024  1.433  0.179538

46  MAC000282   4457.989001  0.030  1.830  0.127444

47  MAC000283   5024.917000  0.011  2.671  0.143627

48  MAC000284   1293.503000  0.000  0.752  0.047975

49  MAC000285   2399.018000  0.006  0.931  0.068567

50  MAC000287   1407.290000  0.000  2.372  0.045253

51  MAC000289   4767.490999  0.000  2.287  0.136436

52  MAC000291  13456.678999  0.072  3.354  0.385060

53  MAC000294   9477.966000  0.053  2.438  0.271264

54  MAC000295   7750.128000  0.010  1.839  0.221774



查看完整回答
反对 回复 2022-11-01
  • 1 回答
  • 0 关注
  • 84 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信