我正在尝试将保存为RData的R中的数据帧导入到pandas数据框中。我怎么能这样做?我没有成功尝试使用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')))
输出:
[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']
我使用pandas 0.20.1 + python 3.6 x64 + Windows 7。
答案 0 :(得分:3)
数据帧的广义转换结果是一项昂贵的操作,因为某些类型的列需要复制。本地转换规则可能会更好:
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
否则,以下是“正常工作”(Linux,rpy2的分支默认负责人):
import pandas as pd
from rpy2.robjects import r
from rpy2.robjects import pandas2ri
pandas2ri.activate()
pd_iris = r('iris')
pd_iris
如果它不适合您,则Windows上的rpy2可能存在问题(另一个问题 - Windows上不完全支持rpy2)。