gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:34:06 24 4
gpt4 key购买 nike

我让 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com