I have a dataset similar to the following:
Date Sentence Text Verified
_
0 2020-01-18 00:00:00 LUKE Diamond is a famous Updates · BREAKING News False
1 2020-01-18 00:00:00 Blog - TASTY YUMMIES Brush with ... False
2 2020-01-18 00:00:00 ACNE Alternative Remedies: Manuka HONEY Learn more from WEBMD False
3 2020-01-18 00:00:00 Looking back at 10 YEARS As the LOCAL community False
I would like select words in upper case that might be in Sentence or in Text (I would like to save these results into two separate columns, rows by rows).
Date Sentence Text Verified CS CT
_
0 2020-01-18 00:00:00 LUKE Diamond is a famous Updates · BREAKING News False ['LUKE'] ['BREAKING']
1 2020-01-18 00:00:00 Blog - TASTY YUMMIES Brush with ... False ['TASTY', 'YUMMIES'] []
2 2020-01-18 00:00:00 ACNE Alternative Remedies: Manuka HONEY Learn more from WEBMD False ['ACNE','HONEY'] ['WEBMD']
3 2020-01-18 00:00:00 Looking back at 10 YEARS As the LOCAL community False ['YEARS'] ['LOCAL']
I have tried as follows (this should generate a list of words in upper cases, rows by rows), but I am getting the following error: TypeError: expected string or bytes-like object in caps.
Could you please tell me what is not working and how I can get the expected output? Many thanks.
import re
def s_l(file):
s = []
for c in file['Sentence'].tolist():
caps = re.findall('([A-Z]+(?:(?!\s?[A-Z][a-z])\s?[A-Z])+)', c)
s.append(caps)
for c in file['Text'].tolist():
caps = re.findall('([A-Z]+(?:(?!\s?[A-Z][a-z])\s?[A-Z])+)', c)
s.append(caps)
file['CS'] = pd.Series(s)
s = [x for x in s if x != [] and len(x)>1]
file['CT'] = pd.Series(s)
s = [x for x in s if x != [] and len(x)>1]
return file, s
s_df, s =s_l(df)
I think I am all wrongly considering a list of words, so probably I should add another list (not only s).