I haven't been able to find an answer here specific to my issue and I'm wondering if I could get some help (apologies for the links, I'm not allowed to embed images yet).
I have stored Counter objects within my DataFrame and also want them added to the DataFrame as a column for each counted element.
Beginning data
data = {
"words": ["ABC", "BCDB", "CDE", "F"],
"stuff": ["abc", "bcda", "cde", "f"]
}
df = pd.DataFrame(data)
patternData = {
"name": ["A", "B", "C", "D", "E", "F"],
"rex": ["A{1}", "B{1}", "C{1}", "D{1}", "E{1}", "F{1}"]
}
patterns = pd.DataFrame(patternData)
def countFound(ps):
result = Counter()
for index, row in patterns.iterrows():
findName = row['name']
findRex = row['rex']
found = re.findall(findRex, ps)
if (len(found) > 0):
result.update({findName:len(found)})
return result
df['found'] = df['words'].apply(lambda x: countFound(x))
| words | stuff | found | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|
| ABC | acb | {'A': 1, 'B': 1, 'C': 1} |
1 | 1 | 1 | 0 | 0 | 0 |
| BCD | bcd | {'B': 1, 'C': 1, 'D': 1} |
0 | 2 | 1 | 1 | 0 | 0 |
| CDE | cde | {'C': 1, 'D': 1, 'E': 1} |
0 | 0 | 1 | 1 | 1 | 0 |
| F | f | {'F': 1} |
0 | 0 | 0 | 0 | 0 | 1 |