dataframe color cells
import pandas as pd
import numpy as np
def highlight_max(s):
is_max = s == s.max()
return ['background-color: red' if v else '' for v in is_max]
df.style.apply(highlight_max)
dataframe color cells
import pandas as pd
import numpy as np
def highlight_max(s):
is_max = s == s.max()
return ['background-color: red' if v else '' for v in is_max]
df.style.apply(highlight_max)
dataframe color cells
import pandas as pd
df = pd.DataFrame({
"name": ["alan","beth","charlie","david", "edward"],
"age" : [34, 12, 43, 32, 77],
"num_children": [1, 0, 2, 1, 6],
"num_pets": [1, 0, 1, 2, 0],
"bank_balance": [100.0, 10.0, -10.0, 30.0, 30.0]})
def more_children_or_more_pets_background(row):
highlight = 'background-color: lightcoral;'
default = ''
# must return one string per cell in this row
if row['num_children'] > row['num_pets']:
return [highlight, default]
elif row['num_pets'] > row['num_children']:
return [default, highlight]
else:
return [default, default]
df.style.apply(more_children_or_more_pets_background, subset=['num_children', 'num_pets'], axis=1)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us