df_head Function

public function df_head(df, n) result(head_df)

Return first n rows as a new dataframe

@param[in] df The data frame to extract from @param[in] n Optional number of rows (default: 6) @return New data frame containing first n rows

Arguments

Type IntentOptional Attributes Name
type(data_frame), intent(in) :: df
integer, intent(in), optional :: n

Return Value type(data_frame)


Source Code

    function df_head(df, n) result(head_df)
        type(data_frame), intent(in) :: df
        integer, intent(in), optional :: n
        type(data_frame) :: head_df

        integer :: num_rows

        if (present(n)) then
            num_rows = min(n, df % nrows())
        else
            num_rows = min(6, df % nrows())  ! Default to 6 rows like pandas
        end if

        if (num_rows == 0 .or. df % nrows() == 0) then
            call head_df % new(df % get_max_char_len())
            return
        end if

        head_df = df_slice_rows(df, 1, num_rows)
    end function df_head