Back to Documentations

Signature Description Parameters
template<comparable T, typename ... Ts>
DataFrame
get_top_n_data(const char *col_name, size_type n) const;
This returns a new DataFrame with the n top rows of the given column. The returned DataFrame rows will be in the same order as self.

NOTE Comparison operators (<, >, ==) must be well defined for type T.
T: Type of the named column
Ts: The list of types for all columns. A type should be specified only once
col_name: Name of the data column
n: Number of top rows
template<comparable T, typename ... Ts>
PtrView
get_top_n_view(const char *col_name, size_type n);
This is identical with above get_top_n_data(), but:
  1. The result is a view
  2. Since the result is a view, you cannot call make_consistent() on the result.
NOTE: There are certain operations that you cannot do with a view. For example, you cannot add/delete columns, etc.
T: Type of the named column
Ts: The list of types for all columns. A type should be specified only once
col_name: Name of the data column
n: Number of top rows
template<comparable T, typename ... Ts>
ConstPtrView
get_top_n_view(const char *col_name, size_type n) const;
Same as above view, but it returns a const view. You can not change data in const views. But if the data is changed in the original DataFrame or through another view, it is reflected in the const view. T: Type of the named column
Ts: The list of types for all columns. A type should be specified only once
col_name: Name of the data column
n: Number of top rows
template<comparable T, typename ... Ts>
DataFrame
get_bottom_n_data(const char *col_name, size_type n) const;
This returns a new DataFrame with the n bottom rows of the given column. The returned DataFrame rows will be in the same order as self.

NOTE Comparison operators (<, >, ==) must be well defined for type T.
T: Type of the named column
Ts: The list of types for all columns. A type should be specified only once
col_name: Name of the data column
n: Number of bottom rows
template<comparable T, typename ... Ts>
PtrView
get_bottom_n_view(const char *col_name, size_type n);
This is identical with above get_bottom_n_data(), but:
  1. The result is a view
  2. Since the result is a view, you cannot call make_consistent() on the result.
NOTE: There are certain operations that you cannot do with a view. For example, you cannot add/delete columns, etc.
T: Type of the named column
Ts: The list of types for all columns. A type should be specified only once
col_name: Name of the data column
n: Number of bottom rows
template<comparable T, typename ... Ts>
ConstPtrView
get_bottom_n_view(const char *col_name, size_type n) const;
Same as above view, but it returns a const view. You can not change data in const views. But if the data is changed in the original DataFrame or through another view, it is reflected in the const view. T: Type of the named column
Ts: The list of types for all columns. A type should be specified only once
col_name: Name of the data column
n: Number of bottom rows
static void test_get_top_n_data()  {

    std::cout << "\nTesting get_top_n_data( ) ..." << std::endl;

    StlVecType<unsigned long>   idx = { 123450, 123451, 123452, 123453, 123454, 123455, 123456, 123457, 123458, 123459, 123460, 123461, 123462, 123466 };
    StlVecType<double>          d1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
    StlVecType<double>          d2 = { 8, 9, 10, 11, 12, 13, 14, 20, 22, 23, 30, 31, 32, 1.89 };
    StlVecType<double>          d3 = { 15, 16, 15, 18, 19, 16, 21, 0.34, 1.56, 0.34, 2.3, 0.34, 19.0, 10 };
    StlVecType<int>             i1 = { 22, 23, 24, 25, 99 };
    MyDataFrame                 df;

    df.load_data(std::move(idx),
                 std::make_pair("col_1", d1),
                 std::make_pair("col_2", d2),
                 std::make_pair("col_3", d3),
                 std::make_pair("col_4", i1));

    auto    lbd = [](const unsigned long &, const double &val) -> bool {
                      return (val < 100.0);
                  };
    auto    view = df.get_view_by_sel<double, decltype(lbd), double, int, std::string>("col_1", lbd);

    auto    res1 = df.get_top_n_data<double, int, double, std::string>("col_3", 4);
    auto    res2 = view.get_top_n_data<double, int, double, std::string>("col_3", 4);
    auto    res3 = df.get_top_n_view<double, int, double, std::string>("col_3", 4);
    auto    res4 = view.get_top_n_view<double, int, double, std::string>("col_3", 4);
    auto    res5 = view.get_top_n_data<unsigned int, int, double, std::string>(DF_INDEX_COL_NAME, 4);

    {
        StlVecType<unsigned long>   out_idx = { 123453, 123454, 123456, 123462 };
        StlVecType<double>          out_col_2 = { 11, 12, 14, 32 };
        StlVecType<double>          out_col_3 = { 18, 19, 21, 19 };
        StlVecType<int>             out_col_4 = { 25, 99, 0, 0 };

        assert(res1.get_index() == out_idx);
        assert(res1.get_column<double>("col_2") == out_col_2);
        assert(res1.get_column<double>("col_3") == out_col_3);
        assert(res1.get_column<int>("col_4") == out_col_4);
    }
    {
        StlVecType<unsigned long>   out_idx = { 123453, 123454, 123456, 123462 };
        StlVecType<double>          out_col_2 = { 11, 12, 14, 32 };
        StlVecType<double>          out_col_3 = { 18, 19, 21, 19 };
        StlVecType<int>             out_col_4 = { 25, 99, 0, 0 };

        assert(res2.get_index() == out_idx);
        assert(res2.get_column<double>("col_2") == out_col_2);
        assert(res2.get_column<double>("col_3") == out_col_3);
        assert(res2.get_column<int>("col_4") == out_col_4);
    }

    res3.write<std::ostream, double, int, std::string>(std::cout, io_format::csv);
    std::cout << std::endl;

    res4.write<std::ostream, double, int, std::string>(std::cout, io_format::csv);
    std::cout << std::endl;

    {
        StlVecType<unsigned long>   out_idx = { 123460, 123461, 123462, 123466 };
        StlVecType<double>          out_col_2 = { 30, 31, 32, 1.89 };
        StlVecType<double>          out_col_3 = { 2.3, 0.34, 19, 10 };
        StlVecType<int>             out_col_4 = { 0, 0, 0, 0 };

        assert(res5.get_index() == out_idx);
        assert(res5.get_column<double>("col_2") == out_col_2);
        assert(res5.get_column<double>("col_3") == out_col_3);
        assert(res5.get_column<int>("col_4") == out_col_4);
    }
}
// -----------------------------------------------------------------------------

static void test_get_bottom_n_data()  {

    std::cout << "\nTesting get_bottom_n_data( ) ..." << std::endl;

    StlVecType<unsigned long>   idx = { 123450, 123451, 123452, 123453, 123454, 123455, 123456, 123457, 123458, 123459, 123460, 123461, 123462, 123466 };
    StlVecType<double>          d1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
    StlVecType<double>          d2 = { 8, 9, 10, 11, 12, 13, 14, 20, 22, 23, 30, 31, 32, 1.89 };
    StlVecType<double>          d3 = { 15, 16, 15, 18, 19, 16, 21, 0.34, 1.56, 0.34, 2.3, 0.34, 19.0, 10 };
    StlVecType<int>             i1 = { 22, 23, 24, 25, 99 };
    MyDataFrame                 df;

    df.load_data(std::move(idx),
                 std::make_pair("col_1", d1),
                 std::make_pair("col_2", d2),
                 std::make_pair("col_3", d3),
                 std::make_pair("col_4", i1));

    auto    lbd = [](const unsigned long &, const double &val) -> bool {
                      return (val < 100.0);
                  };
    auto    view = df.get_view_by_sel<double, decltype(lbd), double, int, std::string>("col_1", lbd);

    auto    res1 = df.get_bottom_n_data<double, int, double, std::string>("col_3", 4);
    auto    res2 = view.get_bottom_n_data<double, int, double, std::string>("col_3", 4);
    auto    res3 = df.get_bottom_n_view<double, int, double, std::string>("col_3", 4);
    auto    res4 = view.get_bottom_n_view<double, int, double, std::string>("col_3", 4);
    auto    res5 = view.get_bottom_n_data<unsigned int, int, double, std::string>(DF_INDEX_COL_NAME, 4);

    {
        StlVecType<unsigned long>   out_idx = { 123457, 123458, 123459, 123461 };
        StlVecType<double>          out_col_2 = { 20, 22, 23, 31 };
        StlVecType<double>          out_col_3 = { 0.34, 1.56, 0.34, 0.34 };
        StlVecType<int>             out_col_4 = { 0, 0, 0, 0 };

        assert(res1.get_index() == out_idx);
        assert(res1.get_column<double>("col_2") == out_col_2);
        assert(res1.get_column<double>("col_3") == out_col_3);
        assert(res1.get_column<int>("col_4") == out_col_4);
    }
    {
        StlVecType<unsigned long>   out_idx = { 123457, 123458, 123459, 123461 };
        StlVecType<double>          out_col_2 = { 20, 22, 23, 31 };
        StlVecType<double>          out_col_3 = { 0.34, 1.56, 0.34, 0.34 };
        StlVecType<int>             out_col_4 = { 0, 0, 0, 0 };

        assert(res2.get_index() == out_idx);
        assert(res2.get_column<double>("col_2") == out_col_2);
        assert(res2.get_column<double>("col_3") == out_col_3);
        assert(res2.get_column<int>("col_4") == out_col_4);
    }

    res3.write<std::ostream, double, int, std::string>(std::cout, io_format::csv);
    std::cout << std::endl;

    res4.write<std::ostream, double, int, std::string>(std::cout, io_format::csv);
    std::cout << std::endl;

    {
        StlVecType<unsigned long>   out_idx = { 123450, 123451, 123452, 123453 };
        StlVecType<double>          out_col_2 = { 8, 9, 10, 11 };
        StlVecType<double>          out_col_3 = { 15, 16, 15, 18 };
        StlVecType<int>             out_col_4 = { 22, 23, 24, 25 };

        assert(res5.get_index() == out_idx);
        assert(res5.get_column<double>("col_2") == out_col_2);
        assert(res5.get_column<double>("col_3") == out_col_3);
        assert(res5.get_column<int>("col_4") == out_col_4);
    }
}

C++ DataFrame