将Julia数据帧转换为Python数据帧

2024-09-30 16:33:25 发布

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

我正在尝试将PyCall.jlwrap('Julia')对象转换为数据帧。我使用PyJulia在Julia中运行一个优化算法,结果是抛出一个dataframe对象。我想将该对象转换为数据帧

这是一个类似于5年前提出的问题。但是,没有任何代码建议如何完成传输

任何帮助都是有用的

这是我目前设置的代码。知道“优化程序”后台发生的事情并没有多大用处,只知道“run\u hybrid”和“run\u storage”命令返回的是一个数据帧:

### load in necessary modules for pyjulia    
from julia import Main as jl 

##load my user defined module
jl.include("optimization_program_v3.jl")

##run function from module
results = jl.run_hybrid(generic_inputs)

##test type of item returned
jl.typeof(results)
returns: <PyCall.jlwrap DataFrame>

##try to convert to pandas
test = pd.DataFrame(results)

Value Error Traceback (most recent call last)

in ()

----> 1 test = pd.DataFrame(results)

in init(self, data, index, columns, dtype, copy)

420 dtype=values.dtype, copy=False) 421 else: 422 raise ValueError('DataFrame constructor not properly called!')

423

424 NDFrame.init(self, mgr, fastpath=True)

ValueError: DataFrame constructor not properly called!


Tags: 数据对象run代码intestdataframeload
1条回答
网友
1楼 · 发布于 2024-09-30 16:33:25

如果使用DataFrames.jl包,则会出现错误(在Python中读取Julia数据帧)。但是,它似乎与Pandas.jl包配合得很好:

>>> from julia import Main as jl
>>> import pandas as pd
>>> jl.eval('using Pandas')
>>> res = jl.eval('DataFrame(Dict(:age=>[27, 29, 27], :name=>["James", "Jill", "Jake"]))')
>>> jl.typeof(res)
#<PyCall.jlwrap PyObject>
>>> df = pd.DataFrame(res)
>>> df
    age   name
0   27  James
1   29   Jill
2   27   Jake

这在Win10、Python 3.8.2和Julia 1.3.1上进行了测试

相关问题 更多 >