Freeze header in pandas dataframe

前端 未结 3 1708
旧时难觅i
旧时难觅i 2020-12-16 12:03

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!!

相关标签:
3条回答
  • 2020-12-16 12:50

    A solution that would work on any editor is to select what rows you want to look at:

    df.ix[100:110] # would show you from row 101 to 110 keeping the header on top
    
    0 讨论(0)
  • 2020-12-16 13:01

    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):

    0 讨论(0)
  • 2020-12-16 13:09

    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.

    0 讨论(0)
提交回复
热议问题