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?
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.