I have a csv file which contains 6 columns for multiple rows. In my case ideally values in c3 should match with value of c5 and c4 with c6. If not the one that is lesser the text in that cell should be highlighted with red.
C1,C2,C3,C4,C5,C6
a,b,21,200,21,200
c,d,1050,1900,1050,1900
k,l,250,1300,250,1330
j,m,230,1400,228,1339
s,t,210,1900,220,1900
Now i read this file using panda.read_csv and convert to html file using .to_html function. But ideally i should also color C4 column with value 1300 as red since it is less than C6 value of 1330 in row3. Similarly color 228 and 1339 as C5 and C6 are less than C3 and C4 respectively for row4.
what i tried to do is basically first read_csv to a pandas dataframe, and then use df.style.apply(fnToColorRed,axis=1) and inside fnToColorRed i tried to color it but it does not work.
def fnToColorRed(value):
color= red if ( value['C2'] != value['C4'] or value['C3'] != value['C5'] ) else black
return 'color %s ' %color
csv_file_df=pd.read_csv("my.csv")
style=csv_file_df.style.apply(fnToColorRed,axis=1)
df_html=style.render()
df_html.to_html('my.html')
But above does not seem to work. In fact i get below error on line: df_html=style.render() which is function returned the wrong shape. ValueError: Function fnToColorRed returned the wrong shape. Result has shape:(5,) Expected shape: (5,6).
Note that i want that html file that is created has the cell color retained as red if it satisfies criteria color= red if ( value['C2'] != value['C4'] or value['C3'] != value['C5'] ) else black
as mentioned in code above.
Thanks
from Shape error when trying to color a cell of dataframe and then dump it as html and trying to retain style for cell
No comments:
Post a Comment