Display Column Names in a Second Row for Improved Readability in Pandas DataFrames

Displaying Column Names in a Second Row of a Pandas DataFrame

When working with large datasets, it can be challenging to view the entire data set at once due to horizontal scrolling. This is particularly problematic when dealing with column names that are long and unwieldy. In this article, we will explore how to display column names in a second row of a pandas DataFrame.

Overview of Pandas DataFrames

A pandas DataFrame is a two-dimensional labeled data structure with columns of potentially different types. It is similar to an Excel spreadsheet or a table in a relational database. DataFrames are widely used in data analysis and scientific computing due to their flexibility, speed, and ease of use.

Creating a Sample DataFrame

To demonstrate the concept, we will create a sample DataFrame using Python’s pandas library.

import pandas as pd

# Create a dictionary with data
data = {
    'hdg1_mv_an2l_mv_carroent_torque_tot': {0: 46.20000076293945,
                                              1: 4.829999923706055,
                                              2: 10.229999542236328,
                                              3: 10.100000381469727,
                                              4: 14.279999732971191},
    'hdg1_mv_an2l_mv_carroent_refvellineal': {0: 12.020000457763672,
                                                1: 15.0,
                                                2: 15.0,
                                                3: 15.0,
                                                4: 15.0},
    'hdg1_mv_an2l_mv_carroent_vellineal': {0: 0.019999999552965164,
                                             1: 14.930000305175781,
                                             2: 2.109999895095825,
                                             3: 14.789999961853027,
                                             4: 4.25},
    'hdg1_ms_chapa_espesor_mm': {0: 320.0, 1: 89.0, 2: 89.0, 3: 89.0, 4: 69.0},
    'hdg1_ms_chapa_ancho': {0: 975.0, 1: 600.0, 2: 600.0, 3: 600.0, 4: 1000.0},
    'hdg1_mv_an2l_mv_carroent_tiro': {0: 3211.0,
                                       1: 208.0,
                                       2: 599.0,
                                       3: 597.0,
                                       4: 876.0},
    'hdg1_ms_do_a706_4_ace_freno': {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 1.0},
    'hdg1_ms_sec_carroentrada_driver': {0: 5.0, 1: 5.0, 2: 5.0, 3: 5.0, 4: 5.0},
    'cumsum_group': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5},
    'thickness_group': {0: 't7', 1: 't3', 2: 't3', 3: 't3', 4: 't2'},
    'final_group': {0: 'grp47',
                    1: 'no_group',
                    2: 'no_group',
                    3: 'no_group',
                    4: 'grp8'}
}

# Create the DataFrame
df = pd.DataFrame(data)

Displaying Column Names in a Second Row

To display column names in a second row, you can use the style attribute of the DataFrame and apply styles to the table. One way to achieve this is by setting the word wrap for the column names.

Setting Word Wrap for Column Names

You can set the word wrap for column names using the following code:

df.style.set_table_styles([{'selector':'th',
                            'props':[('word-wrap', ' break-word'),
                                     ('max-width','50px'),
                                     ( 'text-align', 'left')
                                    ]},
                           {'selector': '.wrap-colnames',
                            'props': [('white-space', 'nowrap')]
                           }])

This code sets the word wrap for column names and removes horizontal scrolling. However, it may not be ideal for all use cases, as it can make the column names difficult to read.

Adjusting Column Name Width

To improve the readability of column names while still displaying them in a second row, you can adjust the width of each column by setting the max-width property:

df.style.set_table_styles([{'selector':'th',
                            'props':[('word-wrap', ' break-word'),
                                     ('max-width','50px'),
                                     ( 'text-align', 'left')
                                    ]},
                           {'selector': '.adjust-colnames',
                            'props': [('max-width', 100)]
                           }])

This code sets the maximum width of each column to 100 pixels, which should provide a good balance between readability and display.

Applying Styles

To apply these styles, you can use the style attribute of the DataFrame. Here’s an example:

print(df.style.set_table_styles([{'selector':'th',
                                  'props':[('word-wrap', ' break-word'),
                                           ('max-width','50px'),
                                           ( 'text-align', 'left')
                                          ]}
                                ]))

This code applies the styles to the table and displays column names in a second row without horizontal scrolling.

Example Use Case

Suppose you have a large DataFrame with many columns, some of which are long and unwieldy. You want to display these column names in a second row without horizontal scrolling, while still maintaining readability.

To achieve this, you can use the following code:

import pandas as pd

# Create a sample DataFrame
data = {
    'Column 1': [1, 2, 3],
    'Longer Column Name 1': [4, 5, 6],
    'Column 2': [7, 8, 9]
}
df = pd.DataFrame(data)

# Apply styles to the table
print(df.style.set_table_styles([{'selector':'th',
                                  'props':[('word-wrap', ' break-word'),
                                           ('max-width','50px'),
                                           ( 'text-align', 'left')
                                          ]}
                                ]))

This code creates a sample DataFrame with three columns, one of which is long and unwieldy. It then applies the styles to the table, displaying column names in a second row without horizontal scrolling.

Conclusion

Displaying column names in a second row can improve readability and reduce horizontal scrolling when working with large DataFrames. By using the style attribute and setting word wrap and maximum width properties, you can achieve this effect while maintaining readability.


Last modified on 2024-04-08