> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Set Caption and Customize Font Size and Color  in Pandas DataFrame
- URL: https://datascientyst.com/set-caption-customize-font-size-color-in-pandas-dataframe/
- Published: 2022-02-01T20:37:19.000Z
- Updated: 2022-02-01T20:37:19.000Z
- Author: John D K
- Tags: Styling

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

```python
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](https://pandas.pydata.org/docs/reference/api/pandas.io.formats.style.Styler.set%5Fcaption.html?ref=datascientyst.com).

Setting new title can accept only string or 2-tuple:

```python
df.style.set_caption('Members')

```

result after the DataFrame renaming:

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

```python
styles = [dict(selector="caption",
            props=[("text-align", "right"),
                   ("font-size", "150%"),
                   ("color", 'lime')])]

df.style.set_caption('Members').set_table_styles(styles)

```

result:

__Members__
|   | Member | Disqualified | Paid |
| - | ------ | ------------ | ---- |
| 0 | John   | 0            | 1    |
| 1 | Bill   | 1            | 0    |

We are free to use HTML and CSS properties like:

- `background-color` \- background color
- `color` \- text colors
- `font-size` \- text sizes
- `text-align` \- text alignment
- `font-family` \- text fonts

To find what properties can be set you can read those two links:

- [HTML Styles](https://www.w3schools.com/html/html%5Fstyles.asp?ref=datascientyst.com)
- [HTML Text Formatting](https://www.w3schools.com/html/html%5Fformatting.asp?ref=datascientyst.com)

## 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")`:

```python
styles = [dict(selector="caption", props=[("font-size", "120%"),
                                          ("font-weight", "bold")])]

df.style.set_caption('Members').set_table_styles(styles)

```

result:

__Members__
|   | 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>`:

```python
styles = [dict(selector="caption", props=[("font-size", "120%")])]

df.style.set_caption('Members<br>52022').set_table_styles(styles)

```

result:

__Members20202__
|   | 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)`:

```python
styles = [dict(selector="caption", props=[("background-color", "cyan")])]

df.style.set_caption('Members').set_table_styles(styles)

```

The new DataFrame have different background:

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

```python
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.