Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to freeze the top row and the first column using XlsxWriter?

I am exporting a pandas DataFrame to Excel, and since it contains a lot of rows and columns, it would be useful to keep the top row and the first column when browsing its contents.

There is a feature present in Excel that allows for freezing the top row and the first column. Is accessible through XlsxWriter when exporting DataFrames to excel?

like image 619
Krzysztof Słowiński Avatar asked Jan 30 '18 10:01

Krzysztof Słowiński


People also ask

Can you freeze both top row and first column?

Freeze columns and rowsSelect the cell below the rows and to the right of the columns you want to keep visible when you scroll. Select View > Freeze Panes > Freeze Panes.

How do I freeze row and first column?

To freeze the first row and column, open your Excel spreadsheet. Select cell B2. Then select the VIEW tab from the toolbar at the top of the screen and click on the Freeze Panes button in the Window group. Then click on the Freeze Panes option in the popup menu.

How do I freeze panes vertically and horizontally at the same time?

To freeze horizontal and vertical headings simultaneously: Select the cell in the upper-left corner of the range you want to remain scrollable. Select View tab, Windows Group, click Freeze Panes from the menu bar. Excel inserts two lines to indicate where the frozen panes begin.


2 Answers

You can use worksheet.freeze_panes() to achieve this . There are many options for that method. Read http://xlsxwriter.readthedocs.io/worksheet.html#worksheet-freeze-panes to know how to use the method.

like image 97
Sandeep Lade Avatar answered Nov 16 '22 22:11

Sandeep Lade


For those who would like to freeze the top row and/or column when exporting a pandas DataFrame to Excel, without interfacing with the underlying engine, to_excel() provides a way to do so via freeze_panes keyword argument.

import pandas as pd
df = pd.DataFrame({"Data": [10, 20, 30, 20, 15, 30, 45]})
df.to_excel("pandas_simple.xlsx", freeze_panes=(1, 1))
like image 7
Matthew Thomas Avatar answered Nov 16 '22 22:11

Matthew Thomas