How to speed up the code - searching through a dataframe takes hours,df.head()