So just looking at your code a bit, I can’t run anything because I don’t have the data. An idea I had that might speed up the process:
Instead of your double for loop where you count the number of times each keyword apeears in the query, you could iterate over all the search queries and build a dictionary (or use counter module) to count the number of times each word appears in total.
# i.e.