Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeze header in pandas dataframe

Tags:

Is there a way by which I can freeze Pandas data frame header { as we do in excel}.So if its a long dataframe with multiple rows we can see the headers once we scroll down!! I am assuming ipython notebook

like image 344
sushmit Avatar asked Feb 28 '15 05:02

sushmit


2 Answers

This function may do the trick:

from ipywidgets import interact, IntSlider from IPython.display import display  def freeze_header(df, num_rows=30, num_columns=10, step_rows=1,                   step_columns=1):     """     Freeze the headers (column and index names) of a Pandas DataFrame. A widget     enables to slide through the rows and columns.      Parameters     ----------     df : Pandas DataFrame         DataFrame to display     num_rows : int, optional         Number of rows to display     num_columns : int, optional         Number of columns to display     step_rows : int, optional         Step in the rows     step_columns : int, optional         Step in the columns      Returns     -------     Displays the DataFrame with the widget     """     @interact(last_row=IntSlider(min=min(num_rows, df.shape[0]),                                  max=df.shape[0],                                  step=step_rows,                                  description='rows',                                  readout=False,                                  disabled=False,                                  continuous_update=True,                                  orientation='horizontal',                                  slider_color='purple'),               last_column=IntSlider(min=min(num_columns, df.shape[1]),                                     max=df.shape[1],                                     step=step_columns,                                     description='columns',                                     readout=False,                                     disabled=False,                                     continuous_update=True,                                     orientation='horizontal',                                     slider_color='purple'))     def _freeze_header(last_row, last_column):         display(df.iloc[max(0, last_row-num_rows):last_row,                         max(0, last_column-num_columns):last_column]) 

Test it with:

import pandas as pd df = pd.DataFrame(pd.np.random.RandomState(seed=0).randint(low=0,                                                            high=100,                                                            size=[200, 50])) freeze_header(df=df, num_rows=10) 

It results in (the colors were customized in the ~/.jupyter/custom/custom.css file): enter image description here

like image 138
Guilherme Beltramini Avatar answered Sep 19 '22 15:09

Guilherme Beltramini


Old question but wanted to revisit it because I recently found a solution. Use the qgrid module: https://github.com/quantopian/qgrid

This will not only allow you to scroll with the headers frozen but also sort, filter, edit inline and some other stuff. Very helpful.

like image 43
Chris Decker Avatar answered Sep 19 '22 15:09

Chris Decker