I have two datasets:
- First dataset represents a table with locomotive information.
- Second dataset represents a table containing exchanged messages between locomotives and control center.
I want to get the number of messages per locomotive that had a delivery time more than or equal to 300 seconds.
In order to calculate this, I wrote the following code (dataframe 1 is 'df_loco', dataframe 2 is 'df_sent'):
for i in range(0, len(df_loco['LOCO']),1):
for j in range(0,len(df_sent['HM_ID_HM']),1):
if (df_sent.iloc[j,1] == df_loco.iloc[i,0]) and (df_sent.iloc[j,11]>=a):
df_loco.iloc[i,4] += 1
The code does the job and gives me the correct number of messages that suffered delays. However, my dataset is big (dataframe 1 is 300+ rows, dataframe 2 is 55.000+) so it takes a long time to execute it.
I have been able to get rid of nested for loops in parts of my code which made it 100x more efficient. But here I'm having issues implementing the 'and' operator while not using 'for' loops. It returns me an error stating that the code is too ambiguous(?).
Question: Is there a way to improve this code, not having to work with nested loops?