[Python] How to change the font color or highlight a string in between to asterisks in python?

Discussão em 'Python' iniciado por Stack, Março 31, 2025.

  1. Stack

    Stack Membro Participativo

    My df has 19 columns but for simplicity the df will have the following columns [['Gene_name','Genes_in_same_transcription_unit']]

    The column 'Gene_name' list a bunch of genes in E. coli and the corresponding entry in the column 'Genes_in_same_transcription_unit' lists the genes that are under the same promoter. An entry in this column contains multiple genes in a list that are separated by a comma and a space (example: pphA, sdsR). I want to know if there is a gene that is listed in the 'Genes_in_same_transcription_unit' that isn't in the 'Gene_name' column and for it to be highlighted or the font color for that gene, and only that gene, be in red or any other color beside black. My ultimate goal is to be able to quickly look at the column 'Genes_in_same_transcription_unit' and see if a whole operon of genes have been affected or if only 1 or 2 genes are affected and where they are in the operon (first gene, middle, or last).

    Below is an example of the modified dataframe

    [​IMG]

    This is what I want to happen

    [​IMG]

    I have tried a bunch of stuff and nothing seems to work. Below is the closest I have gotten to getting what I want. With the code below I can make a new column called 'Highlighted_Genes' where the genes that should be in red be placed between two asterisks and then highlight what is in between the two asterisks in red but when I save the file the font color styling doesn't get saved.

    # Convert Gene_name column to a set for quick lookup
    gene_set = set(df['Gene_name'].dropna()) # Drop NaNs from Gene_name to avoid issues

    # Function to highlight genes
    def highlight_genes(row):
    genes_str = row['Genes_in_same_transcription_unit']

    # Handle NaN or missing values
    if pd.isna(genes_str):
    return None # Keep NaNs as is

    genes_list = genes_str.split(', ') # Split into list

    # Add asterisks to genes NOT in the Gene_name column
    highlighted_list = [f"*{gene}*" if gene not in gene_set else gene for gene in genes_list]

    return ', '.join(highlighted_list) # Join back into string

    # Apply function
    df['Highlighted_Genes'] = df.apply(highlight_genes, axis=1)

    # Function to highlight words inside asterisks
    def highlight_genes(val):
    if not isinstance(val, str): # Ensure it's a string
    return val # Return as is (preserves NaN or other values)

    def replace_func(match):
    return f'<span style="color: red;">{match.group(1)}</span>'

    # Replace text between * * with a red-colored span tag
    highlighted_text = re.sub(r'\*(.*?)\*', replace_func, val)

    return highlighted_text

    # Apply the function using Styler
    df_styled = df.style.format({'Highlighted_Genes': lambda x: highlight_genes(x)})

    # Display in Jupyter Notebook
    df_styled

    df_styled.to_excel('highlighted_genes.xlsx')

    Continue reading...

Compartilhe esta Página