0

I need to highlight (underline/italic/Bold/Color) some words present in a text based on the list for each row arranged in a pandas dataframe. something like this. The list has the words in lower case only, but the words in text can be of lower/title/uppercase.In stackoverflow i could not make the words italic so put stars to highlight.

import pandas as pd
data = [[1, 'I love *Mangoes*','['Mangoes]'], [2,'Along with *mangoes* i like *Oranges* as well','['mangoes',
'oranges']'], [3,'My brother neither likes *Mangoes* nor *Oranges*','['mangoes','oranges']']
df = pd.DataFrame(data, columns = ['ID', 'Text','List_of_words'])
anky
  • 74,114
  • 11
  • 41
  • 70
Basudev
  • 135
  • 8
  • 1
    The dataframe isnot reproducible (it runs into errors) , can you please fix that – anky Jan 14 '20 at 15:44
  • Where shall the highlighting happen? Is the text going to be in an HTML, markdown or anything like that? Because in simple dataframe, you can only highlight whole cells, i guess. See https://stackoverflow.com/questions/49961211/python-pandas-highlight-matching-text-and-row – chefhose Jan 14 '20 at 16:27
  • Yes, but can HTML be represented in a data frame? I need to produce the output in a dB table. Please advise – Basudev Jan 15 '20 at 05:22
  • something like pandas styling – Basudev Jan 22 '20 at 16:36
  • Do you only need to add ``/`` around the words in the `List_of_words` column, or is the word list universal for all data? – Wiktor Stribiżew Mar 14 '20 at 21:32
  • The preference is * or ^ like characters if possible. But if not then anything will do – Basudev Mar 15 '20 at 07:40
  • Try `df['Text'].str.replace(r'(?i)mangoes|oranges', r'*\g<0>*')` – Wiktor Stribiżew Mar 15 '20 at 23:38
  • Please let know if it works or not that way. I suggest editing the question to make the sample code work (it does not compile now) and provide expected output. – Wiktor Stribiżew Mar 23 '20 at 11:19
  • It did not work, and I have lots of words in per row. for sample i have given Mangoes and Oranges. Can we iterate trough each row and pick the words from the list column and highlight them in the Text column – Basudev Mar 23 '20 at 18:56

0 Answers0