How to color a boolean variable (like TRUE in red and FALSE in black) in entire dataframe









up vote
2
down vote

favorite
1












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!










share|improve this question









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 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














up vote
2
down vote

favorite
1












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!










share|improve this question









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 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












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





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!










share|improve this question









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






share|improve this question









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.











share|improve this question









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.









share|improve this question




share|improve this question








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 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












  • 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










  • @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












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)


pic



If want also remove index values:



dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx', index=False)


pic1






share|improve this answer






















  • 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 - 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




    Its working now, Thank you soo much:)
    – pytorch
    14 hours ago

















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)





share|improve this answer






















  • @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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






pytorch is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















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






























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)


pic



If want also remove index values:



dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx', index=False)


pic1






share|improve this answer






















  • 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 - 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




    Its working now, Thank you soo much:)
    – pytorch
    14 hours ago














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)


pic



If want also remove index values:



dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx', index=False)


pic1






share|improve this answer






















  • 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 - 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




    Its working now, Thank you soo much:)
    – pytorch
    14 hours ago












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)


pic



If want also remove index values:



dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx', index=False)


pic1






share|improve this answer














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)


pic



If want also remove index values:



dataframe.style.
applymap(color_negative_red).
to_excel('df.xlsx', index=False)


pic1







share|improve this answer














share|improve this answer



share|improve this answer








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 - 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




    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






  • 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







  • 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












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)





share|improve this answer






















  • @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














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)





share|improve this answer






















  • @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












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)





share|improve this answer














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)






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • @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










pytorch is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















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.













 


draft saved


draft discarded














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














































































Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ