类 pandas.dataframe 的 C++实现

dataframe-cpp

在Python上使用dataframe做数据分析是非常便利的,但是c++端就没有这么幸运了,暂时没有官方的api供我们使用,所以博主通过前段时间的编写的代码,修改了一个在c++端使用的dataframe API。当然经验有限,尚未考虑时间消耗(效率问题),不过对于博主目前的软件使用尚且可以满足,如果需要的话可以点击 dataframe-cpp 跳转码云自行下载,下面我便对于该API进行详细的讲解,以便读者快速使用。

dataframe 的 C++实现的功能有

  • 从csv文件读入
  • 写入csv文件
  • 按列标准化数据
  • 获取一列&一行数据
  • 插入&删除一行数据
  • 插入&删除一列数据
  • 通过列或行拼接两个dataframe类
  • 支持单变量有多种类型,包括 char, int, long int, float, double, std::string

编译环境: c++ 11 to 17

快速使用

下面是一段示例代码,讲解如何使用API操作Dataframe,最后一次更新时间 2021.4.24

#include "dataframe.hpp"

int main() {
    using namespace flame;
    // create an empty dataframe object
    dataframe d1;

    // recreate a dataframe object from csv file or another
    d1.read_csv("../test"); // d1 = std::move(dataframe<double>("../test.txt"));

    // create a dataframe object from csv file
    dataframe d2("../test");

    // concat double dataframe object vertically
    auto d3 = d1 + d2;

    // insert one column from std::vector<T>
    /* Note: only the column will be added automatically when name string of its is not detected.
    This feature does not exist for rows */
    d3["h"] = d3["f"] = d3["g"] = {6, 7, 8, 9};

    // remove one column by str
    std::cout << ((d3.remove("g") ? "successfully" : "unsuccessfully") + std::string(" deleted a colunm!"))
              << std::endl;

    // append one row from std::vector<T>
    d3.append(std::vector<user_variant>(d3.column_num()));

    // change data in ith row
    d3[4] = {6, 7, 8, 9, 10};

    // concat double dataframe object horizontally
    d3.concat_row(d3);

    // change one item
    d3["f"][3] = 2;

    // insert std::vector<T> into dataframe directly
    d3.insert("i", {6.6f, '7', 8, "hello", 10});

    // remove one row by index
    std::cout << ((d3.remove(2) ? "successfully" : "unsuccessfully") + std::string(" deleted a row!")) << std::endl;

    // print dataframe
    std::cout << d3;

    // min_max_scaler<double> scaler(d3); // min max scaler
    toolbox::standard_scaler scaler(d3); // standard scaler

    // save scaler into file
    scaler.save_scaler("../scaler");

    // load scaler from file
    scaler.load_scaler("../scaler");

    // transform dataset
    scaler.transform(d3);

    std::cout << "after scaler : " << std::endl << d3;

    // write into csv file
    d3.to_csv("../final", ',');

    return 0;
}

dataframe 类的完整代码:

完整代码较长在GitHub/码云,如果有兴趣的朋友欢迎去码云github一起维护。

原文链接: https://www.cnblogs.com/FlameBlog/p/14715213.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    类 pandas.dataframe 的 C++实现

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/209951

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 上午12:06
下一篇 2023年2月13日 上午12:06

相关推荐