1

在以下链接中,我们看到我们可以使用tablePython 中的包以 orgtbl 格式返回。

#+begin_src python :results value raw :output :return tabulate(df, headers=df.columns, tablefmt='orgtbl')
from tabulate import tabulate
import pandas as pd
df = pd.DataFrame({
    "a": [1,2,3],
    "b": [4,5,6]
})
#+end_src

#+RESULTS:
|   | a | b |
|---+---+---|
| 0 | 1 | 4 |
| 1 | 2 | 5 |
| 2 | 3 | 6 |

https://emacs.stackexchange.com/questions/28715/get-pandas-data-frame-as-a-table-in-org-babel

但是,我们如何在 Julia 中做到这一点?我知道可以利用 PyCall 导入和使用 Python 库。但是,我似乎无法完成这一任务。

到目前为止我所拥有的:

#+begin_src julia :session main :result output
tabulate.tabulate
#+end_src

#+RESULTS:
: PyObject <function tabulate at 0x7f182faaab80>

尝试输出调用 tabulate.tabulate (不起作用),

#+begin_src julia :session main :return tabulate.tabulate(py"df_raw, headers=df_raw.columns, tablefmt='orgtbl'")
  @chain df_raw begin
      groupby([:target, :sex])
      combine(nrow)
  end
#+end_src

也不起作用, tabulate.tabulate(py"df_raw, headers=df_raw.columns, tablefmt='orgtbl'")或者tabulate.tabulate(df_raw)

出现以下错误:

julia> tabulate.tabulate(df_raw)
ERROR: (in a Julia function called from Python)
JULIA: AbstractDataFrame is not iterable. Use eachrow(df) to get a row iterator or eachcol(df) to get a column iterator
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:33
  [2] iterate(#unused#::DataFrame)
    @ DataFrames ~/.julia/packages/DataFrames/vuMM8/src/abstractdataframe/iteration.jl:23

注意:df_raw 是一个随机数据帧,

help?> df_raw
search: df_raw dof_residual default_cgrad

  No documentation found.

  df_raw is of type DataFrame.

  Summary
  ≡≡≡≡≡≡≡≡≡

  struct DataFrame <: AbstractDataFrame

  Fields
  ≡≡≡≡≡≡≡≡

  columns  :: Vector{AbstractVector{T} where T}
  colindex :: DataFrames.Index

  Supertype Hierarchy
  ≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡

  DataFrame <: AbstractDataFrame <: Any
4

1 回答 1

0

我也在寻找那个。如果您想要的只是 Julia 中的 DataFrame 并且不介意第三方解决方案。有了这个包https://github.com/frederic-santos/ob-ess-julia,你可以

#+begin_src ess-julia :session *julia*
using DataFrames
df = DataFrame(A=1:3, B=4:6)
df
#+end_src 
于 2021-12-22T01:15:06.217 回答