im quite new to pandas and wondered if anyone could help with the below;
I'm trying to use pandas to loop row by row over a dataframe, and for each row I want to compare that row to every row of another dataframe (which is roughly of len 7).
The dataframe is quite sizeable (index is 10min freq from may to october) and the nested for loop I have in place takes an age to run (about 20mins);
frame['Group1 ON With Exception'] = ''
for i in range(len(frame)):
for j in range(len(grp1_extpn_tbl)):
if ((frame.ix[i,'T01\n(kWh) ':'T22\n(kWh) ']>1) == (grp1_extpn_tbl.loc[j]>0)).all():
frame.ix[i,'Group1 ON With Exception'] = ''
break
else:
frame.ix[i,'Group1 ON With Exception'] = 'NOT VALID GROUP1 DATA'
Obviously with pandas the key is to avoid looping and so I have come up with using nested np.where's, which considerably speeds things up (something like 3mins). The issue is it looks quite a cumbersome block of code and I wondered if there was another alternative, or even to condense this block of code more than it is? ;
frame['Group1 ON With Exception'] = ''
frame['Group1 ON With Exception'] = np.where((frame.loc[:,'T01\n(kWh) ':'T22\n(kWh) ']).apply(lambda x: ((x>1) == (grp1_extpn_tbl.loc[0] > 0)).all(), axis=1),'',
np.where((frame.loc[:,'T01\n(kWh) ':'T22\n(kWh) ']).apply(lambda x: ((x>1) == (grp1_extpn_tbl.loc[1] > 0)).all(), axis=1),'',
np.where((frame.loc[:,'T01\n(kWh) ':'T22\n(kWh) ']).apply(lambda x: ((x>1) == (grp1_extpn_tbl.loc[2] > 0)).all(), axis=1),'',
np.where((frame.loc[:,'T01\n(kWh) ':'T22\n(kWh) ']).apply(lambda x: ((x>1) == (grp1_extpn_tbl.loc[3] > 0)).all(), axis=1),'',
np.where((frame.loc[:,'T01\n(kWh) ':'T22\n(kWh) ']).apply(lambda x: ((x>1) == (grp1_extpn_tbl.loc[4] > 0)).all(), axis=1),'',
np.where((frame.loc[:,'T01\n(kWh) ':'T22\n(kWh) ']).apply(lambda x: ((x>1) == (grp1_extpn_tbl.loc[5] > 0)).all(), axis=1),'',
np.where((frame.loc[:,'T01\n(kWh) ':'T22\n(kWh) ']).apply(lambda x: ((x>1) == (grp1_extpn_tbl.loc[6] > 0)).all(), axis=1),'','NOT VALID GROUP1 DATA')))))))
Hopefully the above is enough information, any help would be greatly appreciated.
Thanks,