【发布时间】: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