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?
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.
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.
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.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With