在 pandas 中组合多种样式 [英] Combine multiple styles in pandas
问题描述
我想问一下是否有可能在熊猫中组合不同的样式格式. 例如,我想一次组合以下4种样式.
I would like to ask if it is possible to combine different style formatting in pandas. For instance I want to combine the following 4 styles at once.
df.style.format({'vm_unity': '{:.2%}'}).bar(subset=['vm_unity'], align='mid', color=['#d65f5f', '#5fba7d'])
df.style.bar(subset=['vm_top'], align='mid', color=['#d65f5f', '#5fba7d'])
df.style.bar(subset=['vm_bot'], align='mid', color=['#d65f5f', '#5fba7d'])
df.style.bar(subset=['Sy'], align='mid', color=['#d65f5f', '#5fba7d'])
我目前执行的方式是将它们合并在一行中,但我认为这不是最优雅的方式
The way that i currently perform it is to combine them in a single line but i believe that it's not the most elegant way of doing it
df.style.format({'vm_unity': '{:.2%}'}).bar(subset=['vm_unity'], align='mid', color=['#d65f5f', '#5fba7d']).bar(subset=['vm_top'], align='mid', color=['#d65f5f', '#5fba7d']).bar(subset=['vm_bot'], align='mid', color=['#d65f5f', '#5fba7d']).bar(subset=['Sy'], align='mid', color=['#d65f5f', '#5fba7d'])
可以请您以更有效的方式下一行吗?
Can you please drop a line for a more effective way?
提前谢谢!
推荐答案
您可以使用python的本机换行符\,然后再换行.您的代码为:
You can use the python's native linebreak \ and break the line afterwards. Your code would be:
df.style.format({'vm_unity': '{:.2%}'})\
.bar(subset=['vm_unity'], align='mid', color=['#d65f5f', '#5fba7d'])\
.bar(subset=['vm_top'], align='mid', color=['#d65f5f', '#5fba7d'])\
.bar(subset=['vm_bot'], align='mid', color=['#d65f5f', '#5fba7d'])\
.bar(subset=['Sy'], align='mid', color=['#d65f5f', '#5fba7d'])
这篇关于在 pandas 中组合多种样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!