I have been running a for loop for 4 days now. I understand that there are better ways to do it, but I needed to run it just once. Is there a way to check the progress without interrupting the loop? I’m terrified to loose all of my progress. This is my code
JavaScript
x
7
1
#scoring the sentences
2
for i in range(len(df.hope)):
3
for word in words:
4
df.hope[i] += df.text[i].count(word)
5
for word_f in words_f:
6
df.fear[i] += df.text[i].count(word_f)
7
Advertisement
Answer
Given the information from your comment, here is the most efficient way of computing it in Python (as far as I know).
JavaScript
1
3
1
df['hope'] = df.text.str.count("(" + "|".join(words) + ")")
2
df['fear'] = df.text.str.count("(" + "|".join(words_f) + ")")
3
Hopefully this will terminate in a reasonable amount of time!
Edit:
I forgot to add that you have to cast the text column to the “string” datatype. Before doing the above, cast the text
column using
JavaScript
1
2
1
df.text = df.text.astype("string")
2