简体   繁体   English

如何将R中保存为RData的数据帧导入到pandas中?

[英]How can I import a data frame from R saved as RData to pandas?

I'm trying to import a data frame from R saved as RData to a pandas data frame. 我正在尝试将R中保存为RData的数据帧导入到pandas数据帧中。 How can I do so? 我怎么能这样做? I unsuccessfully tried using rpy2 as follows: 我没有成功尝试使用rpy2如下:

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

# I use iris for convenience but I could have done r.load('my_data.RData')
print(r.data('iris'))
print(r['iris'].head())
print(type(r.data('iris')))

print(pandas2ri.ri2py_dataframe(r.data('iris')))
print(pandas2ri.ri2py(r.data('iris')))
print(pd.DataFrame(r.data('iris')))

outputs: 输出:

[1] "iris"

   Sepal.Length  Sepal.Width  Petal.Length  Petal.Width Species
1           5.1          3.5           1.4          0.2  setosa
2           4.9          3.0           1.4          0.2  setosa
3           4.7          3.2           1.3          0.2  setosa
4           4.6          3.1           1.5          0.2  setosa
5           5.0          3.6           1.4          0.2  setosa
<class 'rpy2.robjects.vectors.StrVector'>
   0  1  2  3
0  i  r  i  s
['iris']

I use pandas 0.20.1 + python 3.6 x64 + Windows 7. 我使用pandas 0.20.1 + python 3.6 x64 + Windows 7。

Generalized conversion of data frames turns out to be an expensive operation as a copying is required for some types of columns. 数据帧的广义转换结果是昂贵的操作,因为某些类型的列需要复制。 A local conversion rule could be better: 本地转换规则可能更好:

from rpy2.robjects import pandas2ri
from rpy2.robjects import default_converter
from rpy2.robjects.conversion import localconverter

print(r.data('iris'))
with localconverter(default_converter + pandas2ri.converter) as cv:
    pd_iris = r('iris')
# this is a pandas DataFrame
pd_iris

Otherwise, the following is "just working" on this end (Linux, head of branch default for rpy2): 否则,以下是“正常工作”(Linux,rpy2的分支默认负责人):

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

pd_iris = r('iris')
pd_iris

If it does not for you, there might be an issue with rpy2 on Windows (yet an other one - rpy2 is not fully supported on Windows). 如果它不适合您,则Windows上的rpy2可能存在问题(还有另一个问题 - 在Windows上不完全支持rpy2)。

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

相关问题 如何将另存为RData的R中的字符向量导入到python列表中? - How can I import a character vector from R saved as RData to a python list? 如何将 Pandas 中保存的数据帧加载为 R 中的 HDF5 文件? - How can I load a data frame saved in pandas as an HDF5 file in R? 如何将Google表格导入熊猫数据框? - How can I import Google sheet into pandas data frame? 如何在计算中使用 Pandas 数据框中的值? - How can I use values from a Pandas data frame in a calcul? 如何将R数据帧导入Pandas? - How can I import R dataframes into Pandas? 我可以将SQL Server(= MS SQL)中的表导入Python / Pandas数据框吗? - Can I import a table from SQL Server (=MS SQL) into a Python / Pandas data frame? Python & Pandas:如何在 (for) 循环中从我的大数据框创建新的小数据框? - Python & Pandas: How can I create new smaller data frames from my large data frame in a (for) loop? 如何将熊猫数据框的第 n 行提取为熊猫数据框? - How can I extract the nth row of a pandas data frame as a pandas data frame? 如何从多个列表的每个唯一组合中创建一个 Pandas 数据框? - How can I create a pandas data frame from each unique combination of multiple lists? 如何从存储在多个嵌套字典中的数据创建 Pandas 框架? - How can I create a Pandas frame from data stored in multiple nested dictionaries?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM