Я сделал здесь вывод МНОГО вещей, но я думаю, что по крайней мере это помещает вас в улучшенное состояние с операционным кодом. 1006 *
import pandas as pd
import re
def find_regex(regex, text, opzione2=None, opzione3=None):
matches_prima = re.findall(regex, text)
lunghezza = len(matches_prima)
if opzione2:
matches_prima2 = re.findall(opzione2, text)
lunghezza += len(matches_prima2)
if opzione3:
matches_prima3 = re.findall(opzione3, text)
lunghezza += len(matches_prima3)
return lunghezza
df = pd.read_csv("data.txt")
print(df)
regex1 = r"cat"
regex2 = r"dog"
regex3 = r"people"
df["CntRegex[1]"] = df["Text"].map(lambda x: find_regex(regex1, x))
df["CntRegex[1&2]"] = df["Text"].map(lambda x: find_regex(regex1, x, regex2))
df["CntRegex[1&2&3]"] = df["Text"].map(lambda x: find_regex(regex1, x, regex2, regex3))
with pd.option_context('display.max_colwidth', 25, "display.max_columns", None):
print(df)
Text CntRegex[1] CntRegex[1&2] CntRegex[1&2&3]
0 Be cxt careful with t... 0 0 0
1 Be cat careful with t... 2 2 2
2 Stop waiting for the ... 1 1 1
3 He found the cat cove... 2 2 2
4 The dogmatic people w... 2 3 4
5 There should have bee... 1 2 2
6 Three people with six... 1 1 2