I have a data set:
| Words | Count |
|---|---|
| Hello,World | |
| World,%,Hello,Germany | |
| Germany,100,ML,Germnay |
My Goal:
I would that the Code does:
- Separate the Words: (
"Hello,World") ---> ("Hello","World") - Lists all separated Words in new columns behind each other
- Count the frequency of Words and put the results in "Count" e.g. it finds two times the world "Hello" in column "Words"
| Words | Counts |
|---|---|
| Hello | 2 |
| World | 2 |
| % | 1 |
| 100 | 1 |
| ML | 1 |
| Germany | 3 |
What I did:
The type of "CL1" is "object"
import pandas as pd
import re
separators = ","
def get_word_len(words: str) -> int:
return len(re.split(separators, words))
df["Count"] = df.Words.apply(get_word_len)
print(df)
But it counts the number of words in every cell and NOT the frequency and count of repetition in columns.