How to Set Caption and Customize Font Size and Color in Pandas DataFrame
1. Overview
In this short guide we will see how to set and customize the caption of the DataFrame styler in Pandas.
We are going to set a new caption, change the format: the font, the font size, the color etc.
2. Setup
For this guide we are going to create a simple DataFrame with the following data:
import pandas as pd
data = {'Member': {0: 'John', 1: 'Bill'},
'Disqualified': {0: 0, 1: 1},
'Paid': {0: 1, 1: 0}}
df = pd.DataFrame(data)
data looks like this:
Member | Disqualified | Paid | |
---|---|---|---|
0 | John | 0 | 1 |
1 | Bill | 1 | 0 |
3. Set new caption for DataFrame
In order set new name for the DataFrame in Pandas we can use the following method pandas.io.formats.style.Styler.set_caption.
Setting new title can accept only string or 2-tuple:
df.style.set_caption('Members')
result after the DataFrame renaming:
Member | Disqualified | Paid | |
---|---|---|---|
0 | John | 0 | 1 |
1 | Bill | 1 | 0 |
As you can see the
If you try to use integer then error is raised:
ValueError:
caption
must be either a string or 2-tuple of strings.
4. Customize the color, font size for caption for DataFrame
To customize the color, font size and text alignment of the caption we can use the set_table_styles()
method. Set:
- set new color - lime
- specify the font-size - 150%
- set text-align - left
styles = [dict(selector="caption",
props=[("text-align", "right"),
("font-size", "150%"),
("color", 'lime')])]
df.style.set_caption('Members').set_table_styles(styles)
result:
Member | Disqualified | Paid | |
---|---|---|---|
0 | John | 0 | 1 |
1 | Bill | 1 | 0 |
We are free to use HTML and CSS properties like:
background-color
- background colorcolor
- text colorsfont-size
- text sizestext-align
- text alignmentfont-family
- text fonts
To find what properties can be set you can read those two links:
5. Set bold or multiline title for DataFrame
In this section we will cover more examples on customizing the DataFrame caption.
5.1. Set bold caption for DataFrame
To change the title or other properties of your DataFrame you can use: ("font-weight", "bold")
:
styles = [dict(selector="caption", props=[("font-size", "120%"),
("font-weight", "bold")])]
df.style.set_caption('Members').set_table_styles(styles)
result:
Member | Disqualified | Paid | |
---|---|---|---|
0 | John | 0 | 1 |
1 | Bill | 1 | 0 |
5.2. Set multiline caption for DataFrame
We can also add HTML tags in the title like the new line HTML tag - <br>
:
styles = [dict(selector="caption", props=[("font-size", "120%")])]
df.style.set_caption('Members<br>52022').set_table_styles(styles)
result:
Member | Disqualified | Paid | |
---|---|---|---|
0 | John | 0 | 1 |
1 | Bill | 1 | 0 |
5.3. Set background for DataFrame title - set_caption()
To change the back-ground color we can use the method .set_table_styles(styles)
:
styles = [dict(selector="caption", props=[("background-color", "cyan")])]
df.style.set_caption('Members').set_table_styles(styles)
The new DataFrame have different background:
topic_1 | topic_2 | topic_3 | topic_4 | |
---|---|---|---|---|
item_1 | a | nan | e | nan |
item_2 | nan | c | nan | nan |
item_3 | nan | d | nan | f |
item_4 | nan | nan | nan | nan |
item_5 | b | nan | nan | d |
6. Get the HTML for the styled DataFrame
Finally if you like to get the HTML code for your customized DataFrame we can use print()
and to_html()
methods:
print(df.style.set_caption('Members').set_table_styles(styles).to_html())
7. Conclusion
We saw how to set custom titles for DataFrame. Also we covered several examples about changing the font size, font, color and other attributes.