Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to change colour of rows in csv using pandas

import numpy as np
import pandas as pd


df = pd.DataFrame([['ex1',8484,-1.15],['ex2',1390,3.5],['ex3',327,10.5],['ex4',1135,3.3],['ex5',6499,2.0],['ex6',1055,4.0]], columns=['tag','data1','dat2'])


def color_rule(tag):
    var1 = df['dat2'][df['tag'] == tag.values[0]]
    return ['background-color: red' if x >= 3 else 'background-color: yellow' for x in var1]

html_column = df.style.apply(color_rule, axis=1, subset=['tag'])

html_column.to_excel('styled.xlsx', engine='openpyxl')
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #change #colour #rows #csv #pandas
ADD COMMENT
Topic
Name
4+3 =