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 :
Thank you
CodePudding user response:
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