简体   繁体   English

如何将 R 的 .rdata 文件加载到 Python 中?

[英]How to load R's .rdata files into Python?

I am trying to convert one part of R code in to Python.我正在尝试将 R 代码的一部分转换为 Python。 In this process I am facing some problems.在这个过程中,我遇到了一些问题。

I have a R code as shown below.我有一个 R 代码,如下所示。 Here I am saving my R output in .rdata format.在这里,我将 R 输出保存为.rdata格式。

nms <- names(mtcars)
save(nms,file="mtcars_nms.rdata")

Now I have to load the mtcars_nms.rdata into Python.现在我必须将 mtcars_nms.rdata 加载到 Python 中。 I imported rpy2 module.我导入了 rpy2 模块。 Then I tried to load the file into python workspace.然后我尝试将文件加载到 python 工作区中。 But could not able to see the actual output.但无法看到实际输出。

I used the following python code to import the .rdata .我使用以下 python 代码导入.rdata

import pandas as pd
from rpy2.robjects import r,pandas2ri
pandas2ri.activate()

robj = r.load('mtcars_nms.rdata')
robj

My python output is我的python输出是

R object with classes: ('character',) mapped to:
<StrVector - Python:0x000001A5B9E5A288 / R:0x000001A5B9E91678>
['mtcars_nms']

Now my objective is to extract the information from mtcars_nms.现在我的目标是从 mtcars_nms 中提取信息。

In R, we can do this by using在 R 中,我们可以使用

load("mtcars_nms.rdata");
get('mtcars_nms')

Now I wanted to do the same thing in Python.现在我想在 Python 中做同样的事情。

There is a new python package pyreadr that makes very easy import RData and Rds files into python:有一个新的 python 包pyreadr可以很容易地将 RData 和 Rds 文件导入 python:

import pyreadr

result = pyreadr.read_r('mtcars_nms.rdata')

mtcars = result['mtcars_nms']

It does not depend on having R or other external dependencies installed.它不依赖于安装 R 或其他外部依赖项。 It is a wrapper around the C library librdata , therefore it is very fast.它是 C 库librdata的包装器,因此速度非常快。

You can install it very easily with pip:您可以使用 pip 轻松安装它:

pip install pyreadr

The repo is here: https://github.com/ofajardo/pyreadr回购在这里: https : //github.com/ofajardo/pyreadr

Disclaimer: I am the developer.免责声明:我是开发人员。

Rather than using the .rdata format, I would recommend to use feather , which allows to efficiently share data between R and Python.我建议使用feather ,而不是使用.rdata格式,它允许在R 和Python 之间有效地共享数据。

In R, you would run something like this:在 R 中,您将运行如下代码:

library(feather)
write_feather(nms, "mtcars_nms.feather")

In Python, to load the data into a pandas dataframe, you can then simply run:在Python中,将数据加载到pandas数据帧,然后你可以简单地运行:

import pandas as pd
nms = pd.read_feather("mtcars_nms.feather")

The R function load will return an R vector of names for the objects that were loaded (into GlobalEnv). R 函数load将返回已加载(到 GlobalEnv 中)的对象的名称的 R 向量。

You'll have to do in rpy2 pretty much what you are doing in R:你必须在 rpy2 中做几乎你在 R 中所做的事情:

R:回复:

get('mtcars_nms')

Python/rpy2蟒蛇/rpy2

robjects.globalenv['mtcars_nms']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM