2

I currently created a Pandas Dataframe from a dictionary. The Dataframe looks something like:

      URL         TITLE
0   /xxxx.xx   Hi this is word count
1   /xxxx.xx   Hi this is Stack Overflow
2   /xxxx.xx   Stack Overflow Questions

I want to add a new column to this table, which lists the number of frequency the word "Stack Overflow" appears. So for example, it would be like:

      URL         TITLE                          COUNT
0   /xxxx.xx   Hi this is word count               0
1   /xxxx.xx   Hi this is Stack Overflow           1
2   /xxxx.xx   Stack Overflow Questions            1

The count function does not seem to work for dictionaries, but only for strings. Would there bean easy way to do this?

4
  • Can you show the dictionary and the code for creating the table Commented Mar 29, 2017 at 7:48
  • 1
    Is this a pandas dataframe? Commented Mar 29, 2017 at 7:54
  • "Stack Overflow" is not a word, it is two words. What if the string contains "Overflow Stack" or "Notstack Overflow"? Commented Mar 29, 2017 at 7:55
  • @Jan Yes it is a dictionary that I converted into a pandas dataframe. I will try to further clarify the question. Commented Mar 29, 2017 at 7:58

2 Answers 2

3

Assuming this is actually a pandas dataframe, you could do:

import pandas as pd

table = {   'URL': ['/xxxx.xx', '/xxxx.xx', '/xxxx.xx'], 
            'TITLE': ['Hi this is word count', 'Hi this is Stack Overflow', 'Stack Overflow Questions']}

df = pd.DataFrame(table)
df['COUNT'] = df.TITLE.str.count('Stack Overflow')
print(df)

This yields:

                       TITLE       URL  COUNT
0      Hi this is word count  /xxxx.xx      0
1  Hi this is Stack Overflow  /xxxx.xx      1
2   Stack Overflow Questions  /xxxx.xx      1
Sign up to request clarification or add additional context in comments.

Comments

1

The count() method on dataframes is good at counting occurrences of a single values such as "Stack Overflow".

To do frequency analysis of multiple values, consider using collection.Counter(data) and its .most_common(k) method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.