类 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一起维护。

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 36
    评论
可以使用Cython或Numba将Python代码编译成C或JIT编译的机器码来加速Pandas.DataFrame函数。以下是使用Cython加速Pandas.DataFrame函数的一些步骤: 1. 安装Cython:使用pip install cython安装Cython。 2. 编写Cython代码:将Pandas.DataFrame函数转化为Cython代码,添加型定义和C语言语法。 3. 编译Cython代码:使用cythonize命令将Cython代码编译成C代码。 4. 构建Python扩展:使用setup.py文件构建Python扩展,将C代码编译成共享库。 5. 使用Python扩展:在Python代码中导入编译好的Python扩展,并使用加速的Pandas.DataFrame函数。 以下是一个使用Cython加速Pandas.DataFrame的示例代码: ``` import pandas as pd import numpy as np import cython # 定义Cython函数 @cython.boundscheck(False) @cython.wraparound(False) @cython.cdivision(True) cpdef pd.DataFrame my_func(pd.DataFrame df): cdef int n_rows = df.shape[0] cdef int n_cols = df.shape[1] cdef np.ndarray[np.float64_t, ndim=2] arr = df.values cdef np.ndarray[np.float64_t, ndim=1] col_sums = np.zeros(n_cols, dtype=np.float64) cdef int i, j # 计算列总和 for i in range(n_rows): for j in range(n_cols): col_sums[j] += arr[i,j] # 创建新的DataFrame new_df = pd.DataFrame(columns=df.columns) new_df.loc[0] = col_sums return new_df # 编译Cython代码 from Cython.Build import cythonize cythonize("my_func.pyx") # 构建Python扩展 from distutils.core import setup from Cython.Build import cythonize setup(ext_modules=cythonize("my_func.pyx")) # 使用Python扩展 import my_func df = pd.DataFrame(np.random.rand(100, 100)) new_df = my_func.my_func(df) ``` 使用Numba加速Pandas.DataFrame函数的步骤与上面似,只需要将Cython替换为Numba即可。
评论 36
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FlameAlpha

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值