> ## 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 Wrap/Break Long Column Names in Pandas Dataframe
- URL: https://datascientyst.com/how-to-wrap-break-long-column-names-in-pandas-dataframe/
- Published: 2025-04-12T11:45:56.000Z
- Updated: 2025-04-12T11:45:56.000Z
- Author: John D K
- Tags: Column

To wrap or break long column names in Pandas we can use module `textwrap` and map the column names with new line symbols:

**(1) Wrap DataFrame column names**

```python
import textwrap
cols_wrap = [textwrap.wrap(x, width=20) for x in df.columns]
cols_wrap = {' '.join(words) : '<br>'.join(words) for words in cols_wrap}
cols_wrap

```

**(2) Truncate column names**

```python
import textwrap
cols_wrap = {x: textwrap.wrap(x, width=15)[1] for x in df.columns}

```

This will prevent formatting issues or horizontal overflow on displayed data. Let's see it in more details and examples:

## Data

Let's use this data:

```python
import pandas as pd

df = pd.DataFrame({
  "Test Data Type N0 extract 1": [0, 1, 2, 3],
  "Test Data Type N101 extract 1": [3, 5, 7, 9],
  "Prod Data Type N0 extract 0": [1, 2, 3, 4],
  "Prod Data Type N101 extract 0": [0.5, 1.0, 1.5, 2.0],
})

```

which will have long names. If you work with 20+ columns this might be visually hard to digest:

|   | Test Data Type N0 extract 1 | Test Data Type N101 extract 1 | Prod Data Type N0 extract 0 | Prod Data Type N101 extract 0 |
| - | --------------------------- | ----------------------------- | --------------------------- | ----------------------------- |
| 0 | 0                           | 3                             | 1                           | 0.5                           |
| 1 | 1                           | 5                             | 2                           | 1.0                           |
| 2 | 2                           | 7                             | 3                           | 1.5                           |
| 3 | 3                           | 9                             | 4                           | 2.0                           |

## 1\. Wrap column names

We can wrap every column name no matter is it OK or too long, by inserting `<br>` to break them:

```python
import textwrap
cols_wrap = [textwrap.wrap(x, width=20) for x in df.columns]
cols_wrap = {' '.join(words) : '<br>'.join(words) for words in cols_wrap}
cols_wrap

```

This will create a dictionary:

```
{'Test Data Type N0 extract 1': 'Test Data Type N0<br>extract 1',
 'Test Data Type N101 extract 1': 'Test Data Type N101<br>extract 1',
 'Prod Data Type N0 extract 0': 'Prod Data Type N0<br>extract 0',
 'Prod Data Type N101 extract 0': 'Prod Data Type N101<br>extract 0'}

```

```python
df.rename(columns=cols_wrap).style.format()

```

Now we can display the DataFrame with wrapped column names:

|   | Test Data Type N0extract 1 | Test Data Type N101extract 1 | Prod Data Type N0extract 0 | Prod Data Type N101extract 0 |
| - | -------------------------- | ---------------------------- | -------------------------- | ---------------------------- |
| 0 | 0                          | 3                            | 1                          | 0.500000                     |
| 1 | 1                          | 5                            | 2                          | 1.000000                     |
| 2 | 2                          | 7                            | 3                          | 1.500000                     |
| 3 | 3                          | 9                            | 4                          | 2.000000                     |

- we can control the lenght of the wrap by - `width=20`
- shorter columns will remain the same
- the `<br>` works in Jupyterlab in combination with `.style.format()`
- original data is unchanged

## 2\. Truncate Column names

We can also break the longer column names by similar approach:

```python
import textwrap
cols_wrap = {x: textwrap.wrap(x, width=15)[1] for x in df.columns}
cols_wrap

```

this time we will have shorter names which consists only from the last part of the wrap:

```
{'Test Data Type N0 extract 1': 'N0 extract 1',
 'Test Data Type N101 extract 1': 'N101 extract 1',
 'Prod Data Type N0 extract 0': 'N0 extract 0',
 'Prod Data Type N101 extract 0': 'N101 extract 0'}

```

result:

|   | N0 extract 1 | N101 extract 1 | N0 extract 0 | N101 extract 0 |
| - | ------------ | -------------- | ------------ | -------------- |
| 0 | 0            | 3              | 1            | 0.5            |
| 1 | 1            | 5              | 2            | 1.0            |
| 2 | 2            | 7              | 3            | 1.5            |
| 3 | 3            | 9              | 4            | 2.0            |

## 3\. Transpose for better vertical readability

If the dataset is small, sometimes transposing helps:

```python
print(df.T.to_string())

```

or by printing:

```python
print(df.T.to_string())

```

This prints the column names as row labels, which makes even long names easier to read:

|                               | 0   | 1   | 2   | 3   |
| ----------------------------- | --- | --- | --- | --- |
| Test Data Type N0 extract 1   | 0.0 | 1.0 | 2.0 | 3.0 |
| Test Data Type N101 extract 1 | 3.0 | 5.0 | 7.0 | 9.0 |
| Prod Data Type N0 extract 0   | 1.0 | 2.0 | 3.0 | 4.0 |
| Prod Data Type N101 extract 0 | 0.5 | 1.0 | 1.5 | 2.0 |

## Resource

- [Tutorials on Pandas DataFrame columns](https://datascientyst.com/column/)
- [Break/wrap long text of column names in Pandas dataframe plain text to\_string output?](https://stackoverflow.com/questions/78129071/break-wrap-long-text-of-column-names-in-pandas-dataframe-plain-text-to-string-ou?ref=datascientyst.com)