如何在MATLAB中将数据帧转换成表?

2024-09-30 04:31:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我让matlabr2019a使用py包装器运行python脚本,它返回一个dataframe。这个dataframe是一个字符串表。有没有办法把pandasdataframe转换成MATLAB表格?你知道吗

目前,我正在将dataframe写入.csv,并将其作为一种解决方法导入MATLAB。你知道吗


Tags: csv方法字符串py脚本dataframe表格matlab
1条回答
网友
1楼 · 发布于 2024-09-30 04:31:46

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

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"    

使用python3.6在R2019a上测试。你知道吗

相关问题 更多 >

    热门问题