df_value_counts_integer Function

public function df_value_counts_integer(df, col_index) result(counts_df)

Count occurrences of each value in an integer column

Returns a data frame with two columns: unique values and their counts

@param[in] df The data frame instance @param[in] col_index Column index (1-based) @return Data frame with columns “Value” and “Count”

Arguments

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

Return Value type(data_frame)


Source Code

    function df_value_counts_integer(df, col_index) result(counts_df)
        type(data_frame), intent(in) :: df
        integer, intent(in) :: col_index
        type(data_frame) :: counts_df

        integer(ik), dimension(:), allocatable :: col, unique_vals, counts
        integer :: i, j

        unique_vals = df_unique_integer(df, col_index)
        allocate (counts(size(unique_vals)))
        counts = 0

        col = df_get_col_integer(df, col_index)
        do i = 1, size(col)
            do j = 1, size(unique_vals)
                if (col(i) == unique_vals(j)) then
                    counts(j) = counts(j) + 1
                    exit
                end if
            end do
        end do

        call counts_df % new()
        call df_append_integer(counts_df, unique_vals, "Value")
        call df_append_integer(counts_df, counts, "Count")
    end function df_value_counts_integer