Cyrine Ezzahra Cyrine Ezzahra - 2 months ago 34
Python Question

selection rows with special conditions

I have this dataframe :

TIMESTAMP equipmeent1 equipement2 class_energy
2016-05-10 04:30:00 107 0 high
2016-05-10 04:40:00 100 90 medium
2016-05-10 04:50:00 106 0 low
2016-05-10 05:00:00 107 0 high


I try to select rows with special condition :

x.loc[x['class_energy'] == 'high', x['TIMESTAMP'] > 2016-05-10 04:30:00 04:10:00,x['TIMESTAMP'] < 2016-05-10 05:00:00 ]


But I get this problem :


IndexingError Traceback (most recent call last)
<ipython-input-241-b47c8396bb9a> in <module>()
----> 1 x.loc[x['class_energy'] == 'high', x['PERIODE_TARIF'] =='HP']

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


in getitem(self, key)
1292
1293 if type(key) is tuple:
-> 1294 return self._getitem_tuple(key)
1295 else:
1296 return self._getitem_axis(key, axis=0)

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


in _getitem_tuple(self, tup)
802 continue
803
--> 804 retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
805
806 return retval

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


in _getitem_axis(self, key, axis)
1437 return self._get_slice_axis(key, axis=axis)
1438 elif is_bool_indexer(key):
-> 1439 return self._getbool_axis(key, axis=axis)
1440 elif is_list_like_indexer(key):
1441

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


in _getbool_axis(self, key, axis)
1301 def _getbool_axis(self, key, axis=0):
1302 labels = self.obj._get_axis(axis)
-> 1303 key = check_bool_indexer(labels, key)
1304 inds, = key.nonzero()
1305 try:

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


in check_bool_indexer(ax, key)
1799 mask = com.isnull(result._values)
1800 if mask.any():
-> 1801 raise IndexingError('Unalignable boolean Series key provided')
1802
1803 result = result.astype(bool)._values

IndexingError: Unalignable boolean Series key provided


Answer

You need to and the conditions using & and use parentheses:

x.loc[(x['class_energy'] == 'high') & (x['TIMESTAMP'] > '2016-05-10 04:30:00') & (x['TIMESTAMP'] < '2016-05-10 05:00:00') ]

It's unclear what you're intending by randomly including 04:10:00 in your original code

you must use & instead of and as we are comparing arrays of values, due to operator precedence the conditions need to be enclosed in parentheses also

What you did was just separate each condition with a , which is meaningless here and caused an evaluation error as it treated your args as a tuple

Also your error x.loc[x['class_energy'] == 'high', x['PERIODE_TARIF'] =='HP'] doesn't match your posted code, if you wanted to use these 2 conditions:

x.loc[(x['class_energy'] == 'high') & (x['PERIODE_TARIF'] =='HP')]

should work