2015-02-28 28 views
9

Có cách nào để tôi có thể đóng băng phần đầu khung dữ liệu Pandas {như chúng ta làm trong excel}. Nếu đó là một khung dữ liệu dài với nhiều hàng, chúng ta có thể thấy tiêu đề khi chúng ta cuộn xuống !! Tôi giả định ipython máy tính xách tayFreeze header in pandas dataframe

+0

bạn đã bao giờ tìm ra điều này chưa? – tmthyjames

+0

Không, tôi không thể tìm ra – sushmit

Trả lời

0

Một giải pháp mà sẽ làm việc trên bất kỳ trình soạn là chọn những gì hàng bạn muốn xem:

df.ix[100:110] # would show you from row 101 to 110 keeping the header on top 
6

Chức năng này có thể làm các 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]) 

thử nghiệm nó với:

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) 

nó là kết quả trong (các màu sắc được tùy chỉnh trong ~/.jupyter/custom/custom.css tệp): enter image description here