Step 4 - Focus the analysis by truncating the clean dataset

A Suggested Function


def f_truncate_df(df, head=5, tail=5):
    n = df.shape[0]

    # Handle rows
    if n > head + tail:
        top_rows = df.iloc[:head, :]
        bottom_rows = df.iloc[-tail:, :]
        middle_row = pd.DataFrame(
            [['...'] * df.shape[1]], 
            index=['...'], 
            columns=df.columns
        )
        df = pd.concat([top_rows, middle_row, bottom_rows])

    # Handle columns
    n_cols = df.shape[1]
    if n_cols > head + tail:
        left_cols = df.iloc[:, :head]
        right_cols = df.iloc[:, -tail:]
        middle_col = pd.DataFrame(
            [['...']] * df.shape[0], 
            index=df.index, 
            columns=['...']
        )
        df = pd.concat([left_cols, middle_col, right_cols], axis=1)

    return df