0

I have a df which looks like this picture of the df

I want to color the row where the value of value column is 5 using pandas? how to do that? For above example, I want to color the John and Linda row.

rowipi
  • 1
  • 1

2 Answers2

0

I think if you want to apply color on particular cells, you can simply use lambda function of pandas:

df.style.applymap(lambda x: "background-color: red" if x>0 else "background-color: white")

Pandas has a Styler feature where you can apply conditional formatting type manipulations to Dataframes. Pandas Documentation.

0
import pandas as pd
df = pd.DataFrame()
df['Name'] = ['John', 'Doe', 'Linda']
df['value'] = [5, 9, 5]
writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
format1 = workbook.add_format({'bg_color': '#FF1717',
                               'font_color': '#990000'})
worksheet.conditional_format(1, 1, len(df), 1,
                             {'type': 'cell',
                              'criteria': '==',
                              'value': 5,
                              'format': format1})
writer.save()

enter image description here

The uploaded picture looks like Excel. It's the way to save on Excel with color.

This page will help you. How to save pandas to excel with different colors

Lazyer
  • 917
  • 1
  • 6
  • 16