zoukankan      html  css  js  c++  java
  • 类 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一起维护。

    任世事无常,勿忘初心
  • 相关阅读:
    Hibernate 学习-3
    Hibernate反向工程使用心得
    MyEclipse中自动整合Spring3+Hibernate/JPA
    jsp页面不显示问题
    jstl获取当前系统时间的方法
    js实现12小时时钟
    从servlet跳到jsp页面,并用jstl 进行判断和显示方法
    jsp调用js文件时出现乱码
    常见异常总结
    js实现表单验证
  • 原文地址:https://www.cnblogs.com/FlameBlog/p/14715213.html
Copyright © 2011-2022 走看看