python - 如何从 MATLAB 中将 pandas 数据框转换为表格?

标签 python pandas matlab dataframe type-conversion

我让 MATLAB R2019a 使用 py 包装器运行 python 脚本,该包装器返回 pandas dataframe。这个dataframe是一个字符串表。有没有办法将 pandas dataframe 转换为 MATLAB 表?

目前,我正在将 dataframe 写入 .csv 并将其导入 MATLAB 作为解决方法。

最佳答案

这可能不是最好的方法,但它可以给你一些新的想法:

function tab = q57081181()
% Import pandas:
pd = py.importlib.import_module('pandas');

% Create a dataframe:
iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv');

% Convert to a table, going throgh dictionary and struct:
st = struct(iris.to_dict());
st2 = structfun( @(x)py.list(x.values), st, 'UniformOutput', false);
tab = struct2table( importMixedData(st2) );


function out = importMixedData(inStruct)
% Import numpy:
np = py.importlib.import_module('numpy');

% Copy fieldnames:
out = inStruct;

% Convert every field separately:
fields = fieldnames(inStruct);
for f = 1:numel(fields)
  fld = fields{f};
  try   % this should work for numeric values:
    out.(fld) = double(np.array(inStruct.(fld))).';
  catch % this should work for text values:
    out.(fld) = string(cell(inStruct.(fld))).';
  end
end

对于输入的情况:

iris = 
  Python DataFrame with properties:

          T: [1×1 py.pandas.core.frame.DataFrame]
         at: [1×1 py.pandas.core.indexing._AtIndexer]
       axes: [1×2 py.list]
     blocks: [1×1 py.dict]
    columns: [1×1 py.pandas.core.indexes.base.Index]
     dtypes: [1×1 py.pandas.core.series.Series]
      empty: 0
     ftypes: [1×1 py.pandas.core.series.Series]
        iat: [1×1 py.pandas.core.indexing._iAtIndexer]
       iloc: [1×1 py.pandas.core.indexing._iLocIndexer]
      index: [1×1 py.pandas.core.indexes.range.RangeIndex]
         ix: [1×1 py.pandas.core.indexing._IXIndexer]
        loc: [1×1 py.pandas.core.indexing._LocIndexer]
       ndim: [1×1 py.int]
       plot: [1×1 py.pandas.plotting._core.FramePlotMethods]
      shape: [1×2 py.tuple]
       size: [1×1 py.numpy.int32]
      style: [1×1 py.pandas.io.formats.style.Styler]
     values: [1×1 py.numpy.ndarray]
    is_copy: [1×1 py.NoneType]
         sepal_length  sepal_width  petal_length  petal_width    species
    0             5.1          3.5           1.4          0.2     setosa
    1             4.9          3.0           1.4          0.2     setosa
    2             4.7          3.2           1.3          0.2     setosa
    3             4.6          3.1           1.5          0.2     setosa
    4             5.0          3.6           1.4          0.2     setosa
    5             5.4          3.9           1.7          0.4     setosa
    6             4.6          3.4           1.4          0.3     setosa
    7             5.0          3.4           1.5          0.2     setosa
    8             4.4          2.9           1.4          0.2     setosa
    ...

我们得到:

tab =
  150×5 table
    sepal_length    sepal_width    petal_length    petal_width      species   
    ____________    ___________    ____________    ___________    ____________
        5.1             3.5            1.4             0.2        "setosa"    
        4.9               3            1.4             0.2        "setosa"    
        4.7             3.2            1.3             0.2        "setosa"    
        4.6             3.1            1.5             0.2        "setosa"    
          5             3.6            1.4             0.2        "setosa"    
        5.4             3.9            1.7             0.4        "setosa"    
        4.6             3.4            1.4             0.3        "setosa"    
          5             3.4            1.5             0.2        "setosa"    
        4.4             2.9            1.4             0.2        "setosa"    

使用 python 3.6 在 R2019a 上进行测试。

关于python - 如何从 MATLAB 中将 pandas 数据框转换为表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57081181/

相关文章:

Python MediaWiki 表正则表达式(查找特定格式的字符串,然后提取其中的子字符串)

python - 我将如何在Perl中读取此数据结构?具有包含包含列表的列表的键的字典/哈希。 Python::Inline给我错误

python - 迭代列的条件以查找其他列值

image-processing - matlab 图像 : return pixel values along the boundingbox in an image?

python - 将Float32格式的数据提取为int32

python - 我可以在不显式序列化的情况下将 python 字典存储在 google 的 BigTable 数据存储中吗?

python - read_csv pandas 函数的输入

python - 从 pandas 数据框列中检索列表元素

arrays - 将一个矩阵的每一行乘以另一个矩阵

python - 什么是 matlab permute(A, [3 2 1]) 在 python 中的等价物?