Back to Documentations

Signature Description Parameters
template<typename T>
void
remove_column(const char *name);
It removes the named column and frees up the memory space.

NOTE: If this is called from a View, the column is removed from the view but not from the original DataFrame
T: Type of the named column
name: Column name
template<typename T>
void
remove_column(std::size_t index);
It removes a column based on index and frees up the memory space.
T: Type of the named column
index: Column index
void
clear();
This removes all the index and data columns but doesn't necessarily free memory space of underlying containers. After this call DataFrame will be empty.
It is very similar to std::vector clear()
void
swap(DataFrame &other);
This swaps all self's index and data columns with the ones in other
It is very similar to std::vector swap()
other: Another DataFrme of the same type
static void test_remove_column()  {

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

    StlVecType<unsigned long>  idx =
        { 123450, 123451, 123452, 123450, 123455, 123450, 123449 };
    StlVecType<double>         d1 = { 1, 2, 3, 4, 5, 6, 7 };
    StlVecType<double>         d2 = { 8, 9, 10, 11, 12, 13, 14 };
    StlVecType<double>         d3 = { 15, 16, 17, 18, 19, 20, 21 };
    StlVecType<int>            i1 = { 22, 23, 24, 25 };
    StlVecType<std::string>    s1 = { "11", "22", "33", "xx", "yy", "gg", "string" };
    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_int", i1),
                 std::make_pair("col_str", s1));

    df.write<std::ostream, double, int, std::string>(std::cout);
    df.remove_column<double>("col_2");
    std::cout << "After removing column `col_2`" << std::endl;
    df.write<std::ostream, double, int, std::string>(std::cout);
    df.remove_column<std::string>("col_str");
    std::cout << "After removing column `col_str`" << std::endl;
    df.write<std::ostream, double, int, std::string>(std::cout);

    StlVecType<double> d22 = { 8, 9, 10, 11, 12, 13, 14 };

    df.load_column<double>("col_2", std::move(d22));
    std::cout << "After adding back column `col_2`" << std::endl;
    df.write<std::ostream, double, int, std::string>(std::cout);
}
// -----------------------------------------------------------------------------

static void test_clear()  {

    std::cout << "\nTesting clear( ) ..." << 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, 17, 18, 19, 20, 21, 0.34, 1.56, 0.34, 2.3, 0.1, 0.89, 0.45 };
    StlVecType<int>    i1 = { 22, 23, 24, 25, 99, 100, 101, 3, 2 };
    StlVecType<std::string>    strvec = { "zz", "bb", "cc", "ww", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn" };
    MyDataFrame        df1;

    df1.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),
                  std::make_pair("str_col", strvec));

    StlVecType<unsigned long>  idx2 =
        { 123450, 123451, 123452, 123453, 123454, 123455, 123456, 123457, 123458, 123459, 123460, 123461, 123462, 123466 };
    StlVecType<double> d12 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
    StlVecType<double> d22 = { 8, 9, 10, 11, 12, 13, 14, 20, 22, 23, 30, 31, 32, 1.89 };
    StlVecType<double> d32 = { 15, 16, 17, 18, 19, 20, 21, 0.34, 1.56, 0.34, 2.3, 0.1, 0.89, 0.45 };
    StlVecType<int>    i12 = { 22, 23, 24, 25, 99, 100, 101, 3, 2 };
    StlVecType<std::string>    strvec2 = { "zz", "bb", "cc", "ww", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn" };
    MyDataFrame        df2;

    df2.load_data(std::move(idx2),
                  std::make_pair("col_1", d12),
                  std::make_pair("col_2", d22),
                  std::make_pair("col_3", d32),
                  std::make_pair("col_4", i12),
                  std::make_pair("str_col", strvec2));

    df1.clear();
    assert(df1.empty());
    assert(df1.shapeless());
    assert(df2.get_index()[4] == 123454);
    assert(df2.get_column<int>("col_4")[7] == 3);
    assert(df2.get_column<std::string>("str_col")[5] == "ff");

    df1 = df2;
    assert(df1.get_index()[4] == 123454);
    assert(df1.get_column<int>("col_4")[7] == 3);
    assert(df1.get_column<std::string>("str_col")[5] == "ff");
}
// -----------------------------------------------------------------------------

static void test_swap()  {

    std::cout << "\nTesting swap( ) ..." << 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, 17, 18, 19, 20, 21, 0.34, 1.56, 0.34, 2.3, 0.1, 0.89, 0.45 };
    StlVecType<int>    i1 = { 22, 23, 24, 25, 99, 100, 101, 3, 2 };
    StlVecType<std::string>    strvec = { "zz", "bb", "cc", "ww", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn" };
    MyDataFrame        df1;
    MyDataFrame        df2;

    df1.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),
                  std::make_pair("str_col", strvec));

    assert(df2.empty());
    assert(df2.shapeless());
    assert(df1.get_index()[4] == 123454);
    assert(df1.get_column<int>("col_4")[7] == 3);
    assert(df1.get_column<std::string>("str_col")[5] == "ff");

    df1.swap(df2);
    assert(df1.empty());
    assert(df1.shapeless());
    assert(df2.get_index()[4] == 123454);
    assert(df2.get_column<int>("col_4")[7] == 3);
    assert(df2.get_column<std::string>("str_col")[5] == "ff");
}

C++ DataFrame