如何在MATLAB中将熊猫数据框转换为表格?

时间:2019-07-17 17:18:18

标签: python pandas matlab dataframe type-conversion

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

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

1 个答案:

答案 0 :(得分:1)

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

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"    

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