Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Catagorise text series based on wherever specific string is contained within text
- example 'I like big red apples' catagory = 'Red Apple'
- """
- # example df
- df = {
- "first_name": ['Bruce', 'Clark', 'Bruce', 'James', 'Nanny', 'Dot'],
- "last_name": ['Lee', 'Kent', 'Banner', 'Bond', 'Mc Phee', 'Cotton'],
- "title": ['mr', 'mr', 'mr', 'mr', 'mrs', 'mrs'],
- "text": ["He is a Kung Fu master", "Wears capes and tight Pants", "Cocktails shaken not stirred", "angry Green man", "suspect scottish accent", "East end legend"],
- "age": [32, 33, 28, 30, 42, 80]
- }
- """
- first_name last_name title text age
- 0 Bruce Lee Mr He is a Kung Fu master 32
- 1 Clark Kent Mr Wears capes and tight Pants 33
- 2 Bruce Banner Mr Cocktails shaken not stirred 28
- 3 James Bond Mr angry Green man 30
- 4 Nanny Mc Phee Mrs suspect scottish accent 42
- 5 Dot Cotton Mrs East end legend 80
- """
- """Create catagory dict
- keys: extracted str that is contained within series text
- vals: mapped assigned catagorys""""
- category_dict = {
- "Kung Fu":"Martial Art",
- "capes":"Clothing",
- "cocktails": "Drink",
- "green": "Colour",
- "scottish": "Scotland",
- "East": "Direction"
- }
- cd= {k.lower(): v for k,v in category_dict.items()} # convert keys to lower case
- # convert the extracted word to lowercase and then map with the lowercase dict
- df['category'] = (
- df['text'].str.extract(
- fr"\b({'|'.join((category_dict.keys()))})\b",
- flags=re.IGNORECASE)[0].str.lower().map(cd)) # add .fillna('Other') to capture unassigned
- # outputs
- """"
- first_name last_name title text age category
- 0 Bruce Lee Mr He is a Kung Fu master 32 Martial Art
- 1 Clark Kent Mr Wears capes and tight Pants 33 Clothing
- 2 Bruce Banner Mr Cocktails shaken not stirred 28 Drink
- 3 James Bond Mr angry Green man 30 Colour
- 4 Nanny Mc Phee Mrs suspect scottish accent 42 Scotland
- 5 Dot Cotton Mrs East end legend 80 Direction
- """"
Advertisement
Comments
-
- Visit https://www.saadashraf.net/ to get web design & development services in UAE.
Add Comment
Please, Sign In to add comment
Advertisement