Python plot groupby.mean

Python plot groupby.mean,python,pandas,dataframe,group-by,mean,Python,Pandas,Dataframe,Group By,Mean,我有下面称为df1_df2的数据帧: IdDeviceTypeNameDevice IdBox IdDeviceValue DateDeviceValue ValueDeviceValue weekday hour value IdDevice 119 48 Chaudière Maud Ferrand 4 536448 2015-11-27 17:54:00 On

我有下面称为df1_df2的数据帧:

IdDeviceTypeNameDevice  IdBox   IdDeviceValue   DateDeviceValue   ValueDeviceValue weekday     hour          value
IdDevice                                    
119 48  Chaudière Maud Ferrand  4   536448  2015-11-27 17:54:00     On          4               17           1
119 48  Chaudière Maud Ferrand  4   536449  2015-11-27 17:54:00     Off         4               17           0
119 48  Chaudière Maud Ferrand  4   536450  2015-11-27 17:54:00     On          4               17           1
119 48  Chaudière Maud Ferrand  4   536451  2015-11-27 17:54:00     Off         4               17           0
119 48  Chaudière Maud Ferrand  4   536453  2015-11-27 18:09:00     On          4               18           1 
我想根据值(在“值”列中)对绘图中的每种设备类型(在“IdDeviceType”列中)进行分组,这些设备类型以“小时”列为轴

这个想法是在一个图上看到一个加热器或另一个设备在白天的几个小时内是开着还是关着的

这就是我所做的:

df1_df2['value']= df1_df2['ValueDeviceValue']
df1_df2.loc[df1_df2['ValueDeviceValue'].str.lower()=='on','value'] = 1.
df1_df2.loc[df1_df2['ValueDeviceValue'].str.lower()=='off','value']= 0.

def my_plot(df,devids,idboxes):
    df = df[df['IdDeviceType'].isin(devids)]
    print (set(df.value.values))

    vals = [df[df['IdBox']== idb].groupby('hour')['value'].mean() for idb in idboxes]
    for val in vals : 
        plt.plot(val)
当我测试它时:

my_plot(df1_df2, [48], [4, 5])
我收到下面的错误消息。看起来我无法
group.by.mean
,因为值列未被识别为数字


DataError回溯(最近一次调用)
在()
---->1我的图(df1\U df2[48],[4,5])
在my_图中(df、设备、IDbox)
4打印(设置(测向值)
5.
6 VAL=[df[df['IdBox']==idb].groupby('hour')['value'].IdBox中idb的平均值()
7对于val中的val:
8#打印(val)
英寸(.0)
4打印(设置(测向值)
5.
6 VAL=[df[df['IdBox']==idb].groupby('hour')['value'].IdBox中idb的平均值()
7对于val中的val:
8#打印(val)
/Users/chloegiraut/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py平均值(self)
962         """
963尝试:
964返回自我。_cython_agg_general('mean'))
965除GroupByError外:
966提高
/Users/chloegiraut/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in_cython_agg_general(仅限self、how、numeric_)
763
764如果len(输出)==0:
765 raise DATABERROR('没有要聚合的数字类型')
766
767返回自包装聚合输出(输出,名称)
DataError:没有要聚合的数字类型

要将值列设为数字,您可以:

# get the On/Off string as 1/0
df1_df2['value'] = (
    df1_df2['ValueDeviceValue'].str.lower() == 'on').astype(np.uint8)
测试代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = [x.strip().split() for x in """
    IdDevice IdDeviceType NameDevice IdBox IdDeviceValue DateDeviceValue ValueDeviceValue weekday hour
    119 48  Chaud 4   536448  2015-11-27T17:54:00     On          4 17
    119 48  Chaud 4   536449  2015-11-27T17:54:00     Off         4 17
    119 48  Chaud 4   536450  2015-11-27T17:54:00     On          4 17
    119 48  Chaud 4   536451  2015-11-27T17:54:00     Off         4 17
    119 48  Chaud 4   536453  2015-11-27T18:09:00     On          4 18
""".split('\n')[1:-1]]
df1_df2 = pd.DataFrame(data=data[1:], columns=data[0])
for column in 'IdDevice IdDeviceType IdBox IdDeviceValue'.split():
    df1_df2[column] = pd.to_numeric(df1_df2[column])

# get the On/Off string as 1/0
df1_df2['value'] = (
    df1_df2['ValueDeviceValue'].str.lower() == 'on').astype(np.uint8)

def my_plot(df, devids, idboxes):
    dev_idx = df['IdDeviceType'].isin(devids)
    df = df[dev_idx]
    print (set(df.value.values))
    vals = [df[df['IdBox'] == idb].groupby('hour')['value'].mean()
            for idb in idboxes]
    for val in vals:
        print()
        print(val)

my_plot(df1_df2, [48], [4, 5])
set([0, 1])

hour
17    0.5
18    1.0
Name: value, dtype: float64

Series([], Name: value, dtype: uint8)
结果:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = [x.strip().split() for x in """
    IdDevice IdDeviceType NameDevice IdBox IdDeviceValue DateDeviceValue ValueDeviceValue weekday hour
    119 48  Chaud 4   536448  2015-11-27T17:54:00     On          4 17
    119 48  Chaud 4   536449  2015-11-27T17:54:00     Off         4 17
    119 48  Chaud 4   536450  2015-11-27T17:54:00     On          4 17
    119 48  Chaud 4   536451  2015-11-27T17:54:00     Off         4 17
    119 48  Chaud 4   536453  2015-11-27T18:09:00     On          4 18
""".split('\n')[1:-1]]
df1_df2 = pd.DataFrame(data=data[1:], columns=data[0])
for column in 'IdDevice IdDeviceType IdBox IdDeviceValue'.split():
    df1_df2[column] = pd.to_numeric(df1_df2[column])

# get the On/Off string as 1/0
df1_df2['value'] = (
    df1_df2['ValueDeviceValue'].str.lower() == 'on').astype(np.uint8)

def my_plot(df, devids, idboxes):
    dev_idx = df['IdDeviceType'].isin(devids)
    df = df[dev_idx]
    print (set(df.value.values))
    vals = [df[df['IdBox'] == idb].groupby('hour')['value'].mean()
            for idb in idboxes]
    for val in vals:
        print()
        print(val)

my_plot(df1_df2, [48], [4, 5])
set([0, 1])

hour
17    0.5
18    1.0
Name: value, dtype: float64

Series([], Name: value, dtype: uint8)

太棒了!stephen!它可以工作,但有一条错误消息我在笔记本中插入了您的代码。它说data=[x.strip().split(),用于“”中的x。split('\n')[1:-1]]对于前两行,它说:indexer:list index out out out range我必须在“”和“”之间做些什么吗“?代码的这一部分只是生成一个数据帧。因此,是的,您需要在”“之间使用某些内容,或者使用您的数据…”。。。