I am trying to use numpy where in conjunction with applymap in pandas.
Sample DF:
f = [[1,5],[20,40],[100,21],[15,19],[-46,101]]
test = pd.DataFrame(f,columns=["A","B"])
test
OP:
A B
0 1 5
1 20 40
2 100 21
3 15 19
4 -46 101
Condition is, if a column value is greater than 50 or less than 25 it should be changed to 0 or it should remain as it is.
Code:
test = test.applymap(lambda x:np.where((test[x]>50)| (test[x]<25), 0,test[x]) )
test
Error:
KeyError Traceback (most recent call last)
~\AppData\Local\Continuum\miniconda\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2896 try:
-> 2897 return self._engine.get_loc(key)
2898 except KeyError:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: (1, 'occurred at index A')
Any suggestions will be helpful