- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用 Rmarkdown 和 reticulate 包将 python 和 R 结合在一起。但是,将 Pandas DataFrame 转换为 R Dataframe 的过程似乎并不一致。
这是一个可重现的示例:
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{python}
import pandas as pd
df = pd.DataFrame({'a':4, 'b':5, 'c':9}, index=[0])
print(df)
```
```{r}
library(reticulate)
df2 <- reticulate::py$df
print(df2)
print(reticulate::py$df)
```
预期结果:
我期望数据帧的粗略渲染(3 次)如下:
## a b c
## 0 4 5 9
## a b c
## 0 4 5 9
## a b c
## 0 4 5 9
实际结果:
import pandas as pd
df = pd.DataFrame({'a':4, 'b':5, 'c':9}, index=[0])
print(df)
## a b c
## 0 4 5 9
library(reticulate)
df2 <- reticulate::py$df
print(df2)
## a b
## 1 <environment: 0x000000001dddb808> <environment: 0x000000001decdc58>
## c
## 1 <environment: 0x000000001e000918>
print(reticulate::py$df)
## a b
## 1 <environment: 0x000000001e807f78> <environment: 0x000000001e8fd480>
## c
## 1 <environment: 0x000000001e9ee608>
```
注意,数据帧从 python 正确打印。一旦我们进入 R,看起来好像 R 数据帧对象已损坏。
这是我的 session 信息:
## R version 3.5.2 (2018-12-20)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 17134)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United States.1252
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] reticulate_1.10.0.9004
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.0 lattice_0.20-38 digest_0.6.16 rprojroot_1.3-2
## [5] grid_3.5.2 jsonlite_1.6 backports_1.1.2 magrittr_1.5
## [9] evaluate_0.11 stringi_1.1.7 Matrix_1.2-15 rmarkdown_1.10
## [13] tools_3.5.2 stringr_1.3.1 yaml_2.2.0 compiler_3.5.2
## [17] htmltools_0.3.6 knitr_1.20
最佳答案
我能够做到这一点,但功能的顺序对我来说有点不同。我很早就将网状包与其他 R 包一起加载。
我在 Python 中完成绝大多数工作,然后将其转换为 R,以使用 DT 包通过 Excel 和 .CSV 导出按钮实现数据 View 。
output:
html_document:
toc: false
toc_depth: 1
---
```{r, loadPython, echo=F}
library(reticulate)
library(tidyverse)
library(DT)
```
```{python, echo=T}
# working with pandas df objects continues from other work
predictions = ts.make_predictions(model,
series + ' SARIMAX',
start=len(train),
end= len(train) + len(oos_exog)-1,
exog_data=oos_exog)
# make the OOS intervals
intervals = ts.get_oos_conf_interval(model=model,
steps_ahead=short_horizon,
exog_data = oos_exog)
# this is raw output
print(intervals)
```
```{r, echo=T}
# convert the pandas df object to R DF
r_df <- reticulate::py$intervals
# make a function to make fancy tables in R Markdown using DT package
makeTable <- function(df, end_col){
datatable(df, extensions = 'Buttons',
options = list(dom = 'Bfrtip',
buttons = list("excel", "csv")
)) %>%
formatRound(columns = c(1:end_col), digits = 0)
}
r_df
# output the table
makeTable(r_df, end_col=4)
```
关于python - 如何在 Rmarkdown 中使用 Reticulate 将 Pandas DataFrame 转换为 R Dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54682446/
假设我有 3 个 DataFrame。其中一个 DataFrame 的列名不在其他两个中。 using DataFrames df1 = DataFrame([['a', 'b', 'c'], [1,
假设我有 3 个 DataFrame。其中一个 DataFrame 的列名不在其他两个中。 using DataFrames df1 = DataFrame([['a', 'b', 'c'], [1,
我有一个 largeDataFrame(多列和数十亿行)和一个 smallDataFrame(单列和 10,000 行)。 只要 largeDataFrame 中的 some_identifier 列
我有一个函数,可以在其中规范化 DataFrame 的前 N 列。我想返回规范化的 DataFrame,但不要管原来的。然而,该函数似乎也会对传递的 DataFrame 进行变异! using D
我想在 Scala 中使用指定架构在 DataFrame 上创建。我尝试过使用 JSON 读取(我的意思是读取空文件),但我认为这不是最佳实践。 最佳答案 假设您想要一个具有以下架构的数据框: roo
我正在尝试从数据框中删除一些列,并且不希望返回修改后的数据框并将其重新分配给旧数据框。相反,我希望该函数只修改数据框。这是我尝试过的,但它似乎并没有做我所除外的事情。我的印象是参数是作为引用传递的,而
我有一个包含大约 60000 个数据的庞大数据集。我会首先使用一些标准对整个数据集进行分组,接下来我要做的是将整个数据集分成标准内的许多小数据集,并自动对每个小数据集运行一个函数以获取参数对于每个小数
我遇到了以下问题,并有一个想法来解决它,但没有成功:我有一个月内每个交易日的 DAX 看涨期权和看跌期权数据。经过转换和一些计算后,我有以下 DataFrame: DaxOpt 。现在的目标是消除没有
我正在尝试做一些我认为应该是单行的事情,但我正在努力把它做好。 我有一个大数据框,我们称之为lg,还有一个小数据框,我们称之为sm。每个数据帧都有一个 start 和一个 end 列,以及多个其他列所
我有一个像这样的系列数据帧的数据帧: state1 state2 state3 ... sym1 sym
我有一个大约有 9k 行和 57 列的数据框,这是“df”。 我需要一个新的数据框:'df_final'- 对于“df”的每一行,我必须将每一行复制“x”次,并将每一行中的日期逐一增加,也就是“x”次
假设有一个 csv 文件如下: # data.csv 0,1,2,3,4 a,3.0,3.0,3.0,3.0,3.0 b,3.0,3.0,3.0,3.0,3.0 c,3.0,3.0,3.0,3.0,3
我只想知道是否有人对以下问题有更优雅的解决方案: 我有两个 Pandas DataFrame: import pandas as pd df1 = pd.DataFrame([[1, 2, 3], [
我有一个 pyspark 数据框,我需要将其转换为 python 字典。 下面的代码是可重现的: from pyspark.sql import Row rdd = sc.parallelize([R
我有一个 DataFrame,我想在 @chain 的帮助下对其进行处理。如何存储中间结果? using DataFrames, Chain df = DataFrame(a = [1,1,2,2,2
我有一个包含 3 列的 DataFrame,名为 :x :y 和 :z,它们是 Float64 类型。 :x 和 "y 在 (0,1) 上是 iid uniform 并且 z 是 x 和 y 的总和。
这个问题在这里已经有了答案: pyspark dataframe filter or include based on list (3 个答案) 关闭 2 年前。 只是想知道是否有任何有效的方法来过
我刚找到这个包FreqTables ,它允许人们轻松地从 DataFrames 构建频率表(我正在使用 DataFrames.jl)。 以下代码行返回一个频率表: df = CSV.read("exa
是否有一种快速的方法可以为 sort 指定自定义订单?/sort!在 Julia DataFrames 上? julia> using DataFrames julia> srand(1); juli
在 Python Pandas 和 R 中,可以轻松去除重复的列 - 只需加载数据、分配列名,然后选择那些不重复的列。 使用 Julia Dataframes 处理此类数据的最佳实践是什么?此处不允许
我是一名优秀的程序员,十分优秀!