How to color a boolean variable (like TRUE in red and FALSE in black) in entire dataframe
up vote
2
down vote
favorite
Customer_id Name Age Balance
Q1 True True True
W2 True True True
E3 True False True
T5 True True False
Y6 True True True
U7 True True True
I8 False False False
O9 True False False
P0 False False False
I want to highlight or color a word 'TRUE' in yellow in the above dataframe
Here is my code which i tried
def color_negative_red(val):
color = 'yellow' if val == 'TRUE' else 'black'
return 'color: %s' % color
df = dataframe.style.
apply(color_negative_red).
to_excel('df.xlsx')
and i am getting the below error
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index Customer_id')
what am i doing wrong here?
please help!
python pandas colors coding-style highlight
New contributor
pytorch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
2
down vote
favorite
Customer_id Name Age Balance
Q1 True True True
W2 True True True
E3 True False True
T5 True True False
Y6 True True True
U7 True True True
I8 False False False
O9 True False False
P0 False False False
I want to highlight or color a word 'TRUE' in yellow in the above dataframe
Here is my code which i tried
def color_negative_red(val):
color = 'yellow' if val == 'TRUE' else 'black'
return 'color: %s' % color
df = dataframe.style.
apply(color_negative_red).
to_excel('df.xlsx')
and i am getting the below error
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index Customer_id')
what am i doing wrong here?
please help!
python pandas colors coding-style highlight
New contributor
pytorch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Hi! What's indataframe.style, can you provide this data? Also please post a dataframe as a code block viaCtrl+kcommand, explanation: meta.stackoverflow.com/questions/251361/…
– Mikhail Stepanov
14 hours ago
@MikhailStepanov I've edited the question.
– pytorch
14 hours ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Customer_id Name Age Balance
Q1 True True True
W2 True True True
E3 True False True
T5 True True False
Y6 True True True
U7 True True True
I8 False False False
O9 True False False
P0 False False False
I want to highlight or color a word 'TRUE' in yellow in the above dataframe
Here is my code which i tried
def color_negative_red(val):
color = 'yellow' if val == 'TRUE' else 'black'
return 'color: %s' % color
df = dataframe.style.
apply(color_negative_red).
to_excel('df.xlsx')
and i am getting the below error
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index Customer_id')
what am i doing wrong here?
please help!
python pandas colors coding-style highlight
New contributor
pytorch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Customer_id Name Age Balance
Q1 True True True
W2 True True True
E3 True False True
T5 True True False
Y6 True True True
U7 True True True
I8 False False False
O9 True False False
P0 False False False
I want to highlight or color a word 'TRUE' in yellow in the above dataframe
Here is my code which i tried
def color_negative_red(val):
color = 'yellow' if val == 'TRUE' else 'black'
return 'color: %s' % color
df = dataframe.style.
apply(color_negative_red).
to_excel('df.xlsx')
and i am getting the below error
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index Customer_id')
what am i doing wrong here?
please help!
python pandas colors coding-style highlight
python pandas colors coding-style highlight
New contributor
pytorch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
pytorch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 14 hours ago
New contributor
pytorch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 15 hours ago
pytorch
195
195
New contributor
pytorch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
pytorch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
pytorch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Hi! What's indataframe.style, can you provide this data? Also please post a dataframe as a code block viaCtrl+kcommand, explanation: meta.stackoverflow.com/questions/251361/…
– Mikhail Stepanov
14 hours ago
@MikhailStepanov I've edited the question.
– pytorch
14 hours ago
add a comment |
1
Hi! What's indataframe.style, can you provide this data? Also please post a dataframe as a code block viaCtrl+kcommand, explanation: meta.stackoverflow.com/questions/251361/…
– Mikhail Stepanov
14 hours ago
@MikhailStepanov I've edited the question.
– pytorch
14 hours ago
1
1
Hi! What's in
dataframe.style, can you provide this data? Also please post a dataframe as a code block viaCtrl+k command, explanation: meta.stackoverflow.com/questions/251361/…– Mikhail Stepanov
14 hours ago
Hi! What's in
dataframe.style, can you provide this data? Also please post a dataframe as a code block viaCtrl+k command, explanation: meta.stackoverflow.com/questions/251361/…– Mikhail Stepanov
14 hours ago
@MikhailStepanov I've edited the question.
– pytorch
14 hours ago
@MikhailStepanov I've edited the question.
– pytorch
14 hours ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Use Styler.applymap instead apply:
dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx')
You can also compare by True if boolean and 'True' if string:
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return 'color: %s' % color
#python 3.6+ with f-strings
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return f'color: color'
#python bellow 3.6
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return 'color: '.format(color)

If want also remove index values:
dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx', index=False)

All enteries of all the columns gets black after executing the above code. The word True did not get colored yellow.
– pytorch
14 hours ago
1
@pytorch - usecolor = 'yellow' if val == True else 'black', because if usecolor = 'yellow' if val else 'black'it also coloring all not False and not 0 values
– jezrael
14 hours ago
1
Its working now, Thank you soo much:)
– pytorch
14 hours ago
add a comment |
up vote
0
down vote
Try this (here you can use apply):
def f(x):
df = x.copy()
for i in df.columns:
df.loc[df[i]=='TRUE',i]=='background-color: yellow'
return df
df=df.style.apply(f, axis=None)
@jezrael Oh. i didn't realize, i'll rollback by edit :-)
– U9-Forward
14 hours ago
1
Background color gets black for both 'True' as well as 'FALSE' using the previous code the one with applymap
– pytorch
14 hours ago
@pytorch I did some stuff wrong and i can't edit now, due to not on computer, so I'll delete mine because i could get down-voted for posting non-working posts, without editing for bit of time
– U9-Forward
14 hours ago
@pytorch Edited comment, so how about now?
– U9-Forward
14 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Use Styler.applymap instead apply:
dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx')
You can also compare by True if boolean and 'True' if string:
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return 'color: %s' % color
#python 3.6+ with f-strings
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return f'color: color'
#python bellow 3.6
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return 'color: '.format(color)

If want also remove index values:
dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx', index=False)

All enteries of all the columns gets black after executing the above code. The word True did not get colored yellow.
– pytorch
14 hours ago
1
@pytorch - usecolor = 'yellow' if val == True else 'black', because if usecolor = 'yellow' if val else 'black'it also coloring all not False and not 0 values
– jezrael
14 hours ago
1
Its working now, Thank you soo much:)
– pytorch
14 hours ago
add a comment |
up vote
2
down vote
accepted
Use Styler.applymap instead apply:
dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx')
You can also compare by True if boolean and 'True' if string:
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return 'color: %s' % color
#python 3.6+ with f-strings
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return f'color: color'
#python bellow 3.6
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return 'color: '.format(color)

If want also remove index values:
dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx', index=False)

All enteries of all the columns gets black after executing the above code. The word True did not get colored yellow.
– pytorch
14 hours ago
1
@pytorch - usecolor = 'yellow' if val == True else 'black', because if usecolor = 'yellow' if val else 'black'it also coloring all not False and not 0 values
– jezrael
14 hours ago
1
Its working now, Thank you soo much:)
– pytorch
14 hours ago
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Use Styler.applymap instead apply:
dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx')
You can also compare by True if boolean and 'True' if string:
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return 'color: %s' % color
#python 3.6+ with f-strings
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return f'color: color'
#python bellow 3.6
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return 'color: '.format(color)

If want also remove index values:
dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx', index=False)

Use Styler.applymap instead apply:
dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx')
You can also compare by True if boolean and 'True' if string:
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return 'color: %s' % color
#python 3.6+ with f-strings
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return f'color: color'
#python bellow 3.6
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return 'color: '.format(color)

If want also remove index values:
dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx', index=False)

edited 14 hours ago
answered 15 hours ago
jezrael
303k20233309
303k20233309
All enteries of all the columns gets black after executing the above code. The word True did not get colored yellow.
– pytorch
14 hours ago
1
@pytorch - usecolor = 'yellow' if val == True else 'black', because if usecolor = 'yellow' if val else 'black'it also coloring all not False and not 0 values
– jezrael
14 hours ago
1
Its working now, Thank you soo much:)
– pytorch
14 hours ago
add a comment |
All enteries of all the columns gets black after executing the above code. The word True did not get colored yellow.
– pytorch
14 hours ago
1
@pytorch - usecolor = 'yellow' if val == True else 'black', because if usecolor = 'yellow' if val else 'black'it also coloring all not False and not 0 values
– jezrael
14 hours ago
1
Its working now, Thank you soo much:)
– pytorch
14 hours ago
All enteries of all the columns gets black after executing the above code. The word True did not get colored yellow.
– pytorch
14 hours ago
All enteries of all the columns gets black after executing the above code. The word True did not get colored yellow.
– pytorch
14 hours ago
1
1
@pytorch - use
color = 'yellow' if val == True else 'black', because if use color = 'yellow' if val else 'black' it also coloring all not False and not 0 values– jezrael
14 hours ago
@pytorch - use
color = 'yellow' if val == True else 'black', because if use color = 'yellow' if val else 'black' it also coloring all not False and not 0 values– jezrael
14 hours ago
1
1
Its working now, Thank you soo much:)
– pytorch
14 hours ago
Its working now, Thank you soo much:)
– pytorch
14 hours ago
add a comment |
up vote
0
down vote
Try this (here you can use apply):
def f(x):
df = x.copy()
for i in df.columns:
df.loc[df[i]=='TRUE',i]=='background-color: yellow'
return df
df=df.style.apply(f, axis=None)
@jezrael Oh. i didn't realize, i'll rollback by edit :-)
– U9-Forward
14 hours ago
1
Background color gets black for both 'True' as well as 'FALSE' using the previous code the one with applymap
– pytorch
14 hours ago
@pytorch I did some stuff wrong and i can't edit now, due to not on computer, so I'll delete mine because i could get down-voted for posting non-working posts, without editing for bit of time
– U9-Forward
14 hours ago
@pytorch Edited comment, so how about now?
– U9-Forward
14 hours ago
add a comment |
up vote
0
down vote
Try this (here you can use apply):
def f(x):
df = x.copy()
for i in df.columns:
df.loc[df[i]=='TRUE',i]=='background-color: yellow'
return df
df=df.style.apply(f, axis=None)
@jezrael Oh. i didn't realize, i'll rollback by edit :-)
– U9-Forward
14 hours ago
1
Background color gets black for both 'True' as well as 'FALSE' using the previous code the one with applymap
– pytorch
14 hours ago
@pytorch I did some stuff wrong and i can't edit now, due to not on computer, so I'll delete mine because i could get down-voted for posting non-working posts, without editing for bit of time
– U9-Forward
14 hours ago
@pytorch Edited comment, so how about now?
– U9-Forward
14 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Try this (here you can use apply):
def f(x):
df = x.copy()
for i in df.columns:
df.loc[df[i]=='TRUE',i]=='background-color: yellow'
return df
df=df.style.apply(f, axis=None)
Try this (here you can use apply):
def f(x):
df = x.copy()
for i in df.columns:
df.loc[df[i]=='TRUE',i]=='background-color: yellow'
return df
df=df.style.apply(f, axis=None)
edited 14 hours ago
answered 15 hours ago
U9-Forward
8,6842733
8,6842733
@jezrael Oh. i didn't realize, i'll rollback by edit :-)
– U9-Forward
14 hours ago
1
Background color gets black for both 'True' as well as 'FALSE' using the previous code the one with applymap
– pytorch
14 hours ago
@pytorch I did some stuff wrong and i can't edit now, due to not on computer, so I'll delete mine because i could get down-voted for posting non-working posts, without editing for bit of time
– U9-Forward
14 hours ago
@pytorch Edited comment, so how about now?
– U9-Forward
14 hours ago
add a comment |
@jezrael Oh. i didn't realize, i'll rollback by edit :-)
– U9-Forward
14 hours ago
1
Background color gets black for both 'True' as well as 'FALSE' using the previous code the one with applymap
– pytorch
14 hours ago
@pytorch I did some stuff wrong and i can't edit now, due to not on computer, so I'll delete mine because i could get down-voted for posting non-working posts, without editing for bit of time
– U9-Forward
14 hours ago
@pytorch Edited comment, so how about now?
– U9-Forward
14 hours ago
@jezrael Oh. i didn't realize, i'll rollback by edit :-)
– U9-Forward
14 hours ago
@jezrael Oh. i didn't realize, i'll rollback by edit :-)
– U9-Forward
14 hours ago
1
1
Background color gets black for both 'True' as well as 'FALSE' using the previous code the one with applymap
– pytorch
14 hours ago
Background color gets black for both 'True' as well as 'FALSE' using the previous code the one with applymap
– pytorch
14 hours ago
@pytorch I did some stuff wrong and i can't edit now, due to not on computer, so I'll delete mine because i could get down-voted for posting non-working posts, without editing for bit of time
– U9-Forward
14 hours ago
@pytorch I did some stuff wrong and i can't edit now, due to not on computer, so I'll delete mine because i could get down-voted for posting non-working posts, without editing for bit of time
– U9-Forward
14 hours ago
@pytorch Edited comment, so how about now?
– U9-Forward
14 hours ago
@pytorch Edited comment, so how about now?
– U9-Forward
14 hours ago
add a comment |
pytorch is a new contributor. Be nice, and check out our Code of Conduct.
pytorch is a new contributor. Be nice, and check out our Code of Conduct.
pytorch is a new contributor. Be nice, and check out our Code of Conduct.
pytorch is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203980%2fhow-to-color-a-boolean-variable-like-true-in-red-and-false-in-black-in-entire%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Hi! What's in
dataframe.style, can you provide this data? Also please post a dataframe as a code block viaCtrl+kcommand, explanation: meta.stackoverflow.com/questions/251361/…– Mikhail Stepanov
14 hours ago
@MikhailStepanov I've edited the question.
– pytorch
14 hours ago