【问题标题】:Reading R dataframes in python in Jupyter在 Jupyter 中的 python 中读取 R 数据帧
【发布时间】:2021-12-13 22:47:55
【问题描述】:

我想在 Jupyter 中将 R 对象读回 python。例如,在 Jupyter 中,此示例读取在 python 中生成并在 R 中处理的数据帧。现在我处理此数据帧并创建一个我希望能够读取到 python 的新数据帧。

Python 单元格:

# enables the %%R magic, not necessary if you've already done this
%load_ext rpy2.ipython

import pandas as pd
df = pd.DataFrame({
    'cups_of_coffee': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    'productivity': [2, 5, 6, 8, 9, 8, 0, 1, 0, -1]
})

R 细胞:

%%R -i df 
# import df from global environment

df$time = 1
df_new = df
df_new

如果我移动到新单元格,则新数据框 df_new 无法读取,因为无法识别。

我试过了:

%Rget df_new

但不知道如何将其分配给 pandas 数据框或将其传递给 python 函数。

如何切换回 python 单元并能够读取在 R 单元中创建的这个新数据框?

【问题讨论】:

    标签: python r jupyter-notebook rpy2


    【解决方案1】:

    所以,我自己随机尝试了一些东西,它奏效了。我找不到一些好的文档。

    所以,你可以简单地做:

    df_python = %Rget df_new
    

    这对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2019-06-21
      • 2015-08-07
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 2021-08-14
      相关资源
      最近更新 更多