将NumPy数组转换为带头文件的Pandas数据框架

  • Post category:Python

将NumPy数组转换为带头文件的Pandas数据框架,需要使用Pandas中的DataFrame函数,该函数将NumPy数组转换为Pandas数据框架的对象,操作步骤如下:

  1. 以numpy数组为数据源,导入Pandas模块并创建数据框架对象。
import pandas as pd
import numpy as np

np_array = np.array([[1, 2], [3, 4], [5, 6]])
df = pd.DataFrame(np_array, columns=['Column1', 'Column2'])
  1. 创建头文件(.h)对象,使用df.to_csv方法将数据框架转换为CSV格式的字符串。指定header参数为True,表示包含头文件。最后写入文件。
with open('data.h', 'w') as f:
    f.write('static const char data[] = ')
    df.to_csv(f, header=True, index=False, sep=',', quotechar='"')
    f.write(';')
  1. 在代码中包含头文件,使用Pandas的read_csv方法将数据文件读取为数据框架。
#include "data.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

std::vector<std::string> split(const std::string& s, char delimiter)
{
    std::vector<std::string> tokens;
    std::string token;
    std::istringstream tokenStream(s);
    while (std::getline(tokenStream, token, delimiter))
    {
        tokens.push_back(token);
    }
    return tokens;
}

int main() {
    std::vector<std::vector<std::string>> data;
    for(auto& line: split(std::string(data), '\n'))
    {
        data.push_back(split(line, ','));
    }
    auto num_rows = data.size() - 1;
    auto num_cols = data[0].size();
    std::vector<std::vector<std::string>> data_body(data.begin() + 1, data.end());
    std::vector<std::vector<int>> data_int(num_rows, std::vector<int>(num_cols, 0));

    for(int r = 0; r < num_rows; r++)
    {
        for(int c = 0; c < num_cols; c++)
        {
            data_int[r][c] = std::stoi(data_body[r][c]);
        }
    }

    auto df = pd.DataFrame(data_int);
    std::cout << df.to_string() << std::endl;
    return 0;
}

示例一:

import pandas as pd
import numpy as np

np_array = np.array([['1', '2'], ['3', '4'], ['5', '6']])
df = pd.DataFrame(np_array, columns=['Column1', 'Column2'])

with open('data.h', 'w') as f:
    f.write('static const char data[] = ')
    df.to_csv(f, header=True, index=False, sep=',', quotechar='"')
    f.write(';')

示例二:

#include "data.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

std::vector<std::string> split(const std::string& s, char delimiter)
{
    std::vector<std::string> tokens;
    std::string token;
    std::istringstream tokenStream(s);
    while (std::getline(tokenStream, token, delimiter))
    {
        tokens.push_back(token);
    }
    return tokens;
}

int main() {
    std::vector<std::vector<std::string>> data;
    for(auto& line: split(std::string(data), '\n'))
    {
        data.push_back(split(line, ','));
    }
    auto num_rows = data.size() - 1;
    auto num_cols = data[0].size();
    std::vector<std::vector<std::string>> data_body(data.begin() + 1, data.end());
    std::vector<std::vector<int>> data_int(num_rows, std::vector<int>(num_cols, 0));

    for(int r = 0; r < num_rows; r++)
    {
        for(int c = 0; c < num_cols; c++)
        {
            data_int[r][c] = std::stoi(data_body[r][c]);
        }
    }

    auto df = pd.DataFrame(data_int);
    std::cout << df.to_string() << std::endl;
    return 0;
}