How to lookup/count values in one column to see if they fall in a date range?

Viewed 68

enter image description here

I am trying to look up/count how many times a word fall sinto a date range based off pandas columns

1 Answers

This might not be the best method, but if I understood your question correctly you can try this: You need to first split the sentences into words to search for words like "this" specifically. I am also assuming that the date column is in the correct format. You can split your sentences into a list of words as follows:

df['list_of_words'] = df['sentences'].apply(lambda x: x.split())

This is a dirty method to do it but will get the work done. There can be better, more pythonic ways for sure.

 word = 'this'
 start_date = '1/2/2018'
 end_date = '1/5/2018'   

 def count_word(word, start_date, end_date):

    count = 0

    for i, word_list in enumerate(df.list_of_words.values):
        if (word in word_list) and ((df.date_col >= start_date) and (df.end_date <= end_date)):
            count += 1

    return count
Related