1

I wanna to make some style (yellow background color) in specific rows if match the condition. And the condition is the value of "Last Modified" column BEFORE 1 month. Anyone can help me to implement with this?

Code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from time import sleep
import datetime
import os 
from bs4 import BeautifulSoup
import pandas as pd
import csv
import logging
import sys

activeUser = os.getlogin()
prevDateTime = datetime.datetime.today() - datetime.timedelta(days = 31)

    csvFile = pd.read_csv ("C:\\Users\\" + activeUser + "\\Documents" + "\\ActiveLog" + "\\issueList.csv")
    csvFile.sort_values(by=["Responsible","ID#"], inplace = True)
    sleep(3)
    csvFile["Arrived Date"] = pd.to_datetime(csvFile["Arrived Date"])
    csvFile["Last Modified"] = pd.to_datetime(csvFile["Last Modified"])
    sleep(3)
    csvFile = csvFile[(csvFile['Arrived Date'] > "2021-01-01") & (csvFile['Arrived Date'] < "2021-12-31")]
    sleep(3)
    csvFile.to_excel ("C:\\Users\\" + activeUser + "\\Documents" + "\\ActiveLog" + "\\issueList.xlsx", header = True, index = None)
    sleep(3)

UPDATE - Expected result

RESULT1

2
  • "Last Modified" column BEFORE 1 month exactly or its between today to Before 1 month. you can use pandas style function for the same. for more guidance visit this document pandas.pydata.org/pandas-docs/stable/user_guide/style.html Commented Mar 24, 2021 at 4:41
  • Thanks for your reply. I have attached the picture which is expected result. Is it possible to draw a background yellow color in entire row if meet up condition? Commented Mar 24, 2021 at 4:58

1 Answer 1

1

Here I am using dateutil.relativedelta to get right 1 month difference as compared to timedelta(days=31)

from dateutil.relativedelta import *
import datetime

def highlight_rows(s):
    if s["Last Modified"]< datetime.datetime.today() - relativedelta(months=1):
        return ['background-color: yellow'] * s.size
    else:
        return [''] * s.size

#before write csvFile to excel put this below line 
    csvFile = csvFile.style.apply(highlight_rows, axis=1)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. Finally, the problem was solved. It's working well. : )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.