0

I want to print data from an Excel document that I imported. Each row comprises a Description and an Ugence level. What I want is to print in red each row that has the "Urgent" statement in it, I made a function which works for that (red_text).

I can print the entire rows in red but I can't find how to only print those with the mention "Urgent" in their Urgence row. Here is my code :

Import of the file

import pandas as pd
from dash import Dash, html, dcc, dash_table

# Importing the file
file = r"test.xlsx"

try:
    df = pd.read_excel(file)

except OSError:
    print("Impossible to read :", file)


# Function to add red color to a text
def red_text(textarea):
    return html.P(
        [
             html.Span(textarea, style={"color": "red"}),
        ]
    )

Iterating through each row to put it into a test[] list and then applying a style to each row

# Loop for each row if there is an urgent statement, put the row in red
test = []
for index, row in df.iterrows():
    x = row['Description'] + ' | ' + row['Urgence']
    test.append(x)
    
    # **HERE** -> Statement to apply a color with the function red_text
    if row['Urgence'] == "Urgent":
        red_text(test)

This last statement prints the full table in red, but I only want to apply the red_text function to the rows with the "Urgent" mention in them (from the "Urgence" row).

Edit : the Excel file is a basic two columns file :

enter image description here

Thank you

Stacky
  • 23
  • 6
  • Can you share the input file, or a at least a part of it? – alec_djinn Aug 20 '22 at 09:27
  • I think you want to call `red_text(x)` instead of `red_text(test)` but without input file I can't test it. – alec_djinn Aug 20 '22 at 09:30
  • It's a basic two columns excel file : edit : i'll put it in the main section for better visibility. – Stacky Aug 20 '22 at 09:30
  • here is how to post a proper pandas example https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – alec_djinn Aug 20 '22 at 09:31
  • I actually need help only for the last statement, the for loop, to print the right rows in color and not all of them colored. – Stacky Aug 20 '22 at 09:34
  • What is `html.P`? – alec_djinn Aug 20 '22 at 09:41
  • It adds a paragraph to the html page with the desired text. – Stacky Aug 20 '22 at 09:43
  • Where si it coming from? You have to post a minimal reproducible example else it is not possible for us to test your code. https://stackoverflow.com/help/minimal-reproducible-example – alec_djinn Aug 20 '22 at 09:46
  • It's from the Dash library, I didn't have enough tags to add it. Well it could be anything really it's more to understand how to apply something to certain rows containing a specific term, here "Urgent". – Stacky Aug 20 '22 at 09:46
  • The point is that I should have been able to copy-paste your code and get an output. Instead I don't get anything because, like it is now, your example code is not reproducible. It lacks imports and the dataframe source. Help us to help you, write a better example anyone can play with and give you suggestions. – alec_djinn Aug 20 '22 at 09:49

1 Answers1

1

Given that I can't verify the output because of the lack of a reproducible example, I think you want to do something like:

df = pd.DataFrame({'Description':['urgent stuff','asdasd','other urgent'],'Urgence':['Urgent','sadasd','Urgent']})
print(df)

urgent_stuff = df.loc[df['Urgence'] == "Urgent"]
print('------------')
print(urgent_stuff)


print('++++++++++++')
for row in urgent_stuff.iterrows():
    red_html = red_text(row) #I am not sure what textarea is supposed to be, nor what html.P is
    print(red_html)

the output is:

    Description Urgence
0  urgent stuff  Urgent
1        asdasd  sadasd
2  other urgent  Urgent
------------
    Description Urgence
0  urgent stuff  Urgent
2  other urgent  Urgent
++++++++++++

NameError: name 'html' is not defined
alec_djinn
  • 10,104
  • 8
  • 46
  • 71