难以在python中创建函数组

时间:2016-09-02 12:36:26

标签: python pandas dataframe

我有一个数据框y:

y.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns (total 17 columns):
TIMESTAMP                       5 non-null datetime64[ns]
P_ACT_KW                        5 non-null int64
PERIODE_TARIF                   5 non-null object
P_SOUSCR                        5 non-null int64
high_energy                     5 non-null int64
medium_energy                   5 non-null int64
low_energy                      5 non-null int64
0ACT_TIME_ETA_PRG_P2REF_RM      5 non-null int64
0ACT_TIME_ETA_PRG_VDES_RM       5 non-null int64
0ACT_TIME_ETA_PRG_P3REF_RM      5 non-null int64
0ACT_TIME_ETA_POMP_RECIRC_N1    5 non-null int64
0ACT_TIME_ETA_POMP_RECIRC_N2    5 non-null int64
0ACT_TIME_ETA_POMP_RECIRC_N3    5 non-null int64
0ACT_TIME_ETA_SURPRES_AIR_N1    5 non-null int64
0ACT_TIME_ETA_SURPRES_AIR_N2    5 non-null int64
0ACT_TIME_ETA_SURPRES_AIR_N3    5 non-null int64
class_energy                    5 non-null object
dtypes: datetime64[ns](1), int64(14), object(2)
memory usage: 720.0+ bytes 

TIMESTAMP P_ACT_KW PERIODE_TARIF P_SOUSCR high_energy medium_energy low_energy 0ACT_TIME_ETA_PRG_P2REF_RM 0ACT_TIME_ETA_PRG_VDES_RM 0ACT_TIME_ETA_PRG_P3REF_RM 0ACT_TIME_ETA_POMP_RECIRC_N1 0ACT_TIME_ETA_POMP_RECIRC_N2 0ACT_TIME_ETA_POMP_RECIRC_N3 0ACT_TIME_ETA_SURPRES_AIR_N1 0ACT_TIME_ETA_SURPRES_AIR_N2 0ACT_TIME_ETA_SURPRES_AIR_N3 class_energy
2016-05-10 04:30:00 107 HP 250 0 107 0 100 0 0 0 0 0 0 0 0 medium
2016-05-10 04:40:00 109 HP 250 0 109 0 0 0 100 0 0 0 0 0 0 medium
2016-05-10 04:50:00 106 HP 250 0 106 0 0 0 100 0 3 0 0 0 0 high

我尝试创建一个函数,用列计算列的总和&#39; class_energy&#39;,为此,我确实喜欢这样:

def sum_proba(data):
    data = (data.groupby(by='class_energy', as_index=False)['0ACT_TIME_ETA_PRG_P2REF_RM', '0ACT_TIME_ETA_PRG_VDES_RM','0ACT_TIME_ETA_PRG_P3REF_RM','0ACT_TIME_ETA_POMP_RECIRC_N1','0ACT_TIME_ETA_POMP_RECIRC_N2', '0ACT_TIME_ETA_POMP_RECIRC_N3','0ACT_TIME_ETA_SURPRES_AIR_N1'], data['0ACT_TIME_ETA_SURPRES_AIR_N2','0ACT_TIME_ETA_SURPRES_AIR_N3'].sum())
    return data

当我测试时,我收到此错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\indexes\base.py
     get_loc中的

(self,key,method,tolerance)          1944年尝试:        - &GT; 1945年返回self._engine.get_loc(键)          1946年除了KeyError:

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)()

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
     

(熊猫\ hashtable.c:12368)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
     

(熊猫\ hashtable.c:12322)()

KeyError: ('0ACT_TIME_ETA_SURPRES_AIR_N2', '0ACT_TIME_ETA_SURPRES_AIR_N3')

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-115-6f698f306092> in <module>()
----> 1 sum_proba(x)

<ipython-input-114-a946896b0480> in sum_proba(data)
      1 def sum_proba(data):
----> 2     data = (data.groupby(by='class_energy', as_index=False)['0ACT_TIME_ETA_PRG_P2REF_RM',
     

&#39; 0ACT_TIME_ETA_PRG_VDES_RM&#39;&#39; 0ACT_TIME_ETA_PRG_P3REF_RM&#39;&#39; 0ACT_TIME_ETA_POMP_RECIRC_N1&#39;&#39; 0ACT_TIME_ETA_POMP_RECIRC_N2&#39 ;,   &#39; 0ACT_TIME_ETA_POMP_RECIRC_N3&#39;&#39; 0ACT_TIME_ETA_SURPRES_AIR_N1&#39],   数据[&#39; 0ACT_TIME_ETA_SURPRES_AIR_N2&#39;&#39; 0ACT_TIME_ETA_SURPRES_AIR_N3&#39]。总和())             3返回数据

C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\frame.py
      getitem 中的

(自我,密钥)          1995年返回self._getitem_multilevel(key)          1996其他:        - &GT; 1997年返回self._getitem_column(关键)          1998年          1999 def _getitem_column(self,key):

C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\frame.py
     _getitem_column中的

(self,key)          2002#get column          2003年如果self.columns.is_unique:        - &GT; 2004返回self._get_item_cache(键)          2005年          2006#duplicate columns&amp;可能降低维度

C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\generic.py
     _get_item_cache中的

(self,item)          1348 res = cache.get(item)          1349如果res为None:        - &GT; 1350个值= self._data.get(item)          1351 res = self._box_item_values(item,values)          1352 cache [item] = res

C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\internals.py
     

in get(self,item,fastpath)          3288          3289如果不是isnull(item):        - &GT; 3290 loc = self.items.get_loc(item)          3291其他:          3292 indexer = np.arange(len(self.items))[isnull(self.items)]

C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\indexes\base.py
     get_loc中的

(self,key,method,tolerance)          1945年返回self._engine.get_loc(键)          1946年除了KeyError:        - &GT; 1947年返回self._engine.get_loc(self._maybe_cast_indexer(key))          1948年          1949年indexer = self.get_indexer([key],method = method,tolerance = tolerance)

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)()

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
     

(熊猫\ hashtable.c:12368)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
     

(熊猫\ hashtable.c:12322)()

KeyError: ('0ACT_TIME_ETA_SURPRES_AIR_N2', '0ACT_TIME_ETA_SURPRES_AIR_N3')

有什么想法帮我解决这个问题?

提前谢谢

0 个答案:

没有答案