pandas 熊猫 - 如何将 r 数据帧转换回熊猫?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20630121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas - how to convert r dataframe back to pandas?
提问by Tampa
I converted a pandas df to r using the the below:
我使用以下内容将Pandas df 转换为 r:
import pandas as pd
import pandas.rpy.common as com
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
rdf = com.convert_to_r_dataframe(df)
How do I convert rdf back to a pandas df?
如何将 rdf 转换回Pandas df?
df = f(rdf) ?
回答by lgautier
Since rpy2 release 2.4.0 converting data frames back and forth between rpy2and pandasis included as an optional module. With it, no need to convert explicitly, it will be done on the fly.
由于 rpy2 版本 2.4.0 来回转换数据帧,rpy2并pandas作为可选模块包含在内。有了它,无需显式转换,即可即时完成。
The documentation contains examples (also available as a Jupyter notebook - link available near the top of the page): https://rpy2.github.io/doc/latest/html/pandas.html#interoperability-with-pandas
该文档包含示例(也可作为 Jupyter 笔记本使用 - 链接位于页面顶部附近):https://rpy2.github.io/doc/latest/html/pandas.html#interoperability-with-pandas
Note:The original answer to this question recommended the following.
注意:此问题的原始答案建议如下。
from rpy2.robjects import pandas2ri
pandas2ri.activate()
If wishing to convert explicitly for any reason, the functions are pandas2ri.py2ri()and pandas2ri.ri2py()(they were pandas2ri.pandas2ri()and pandas2ri.ri2pandas()).
如果出于任何原因希望显式转换,则函数是pandas2ri.py2ri()and pandas2ri.ri2py()(它们是pandas2ri.pandas2ri()and pandas2ri.ri2pandas())。
Note:Since rpy2 release 3.3.0 explicit conversion is done as follows
注意:从 rpy2 release 3.3.0 开始显式转换如下
import rpy2.robjects as ro
dt = pd.DataFrame()
# To R DataFrame
r_dt = ro.conversion.py2rpy(dt)
# To pandas DataFrame
pd_dt = ro.conversion.rpy2py(r_dt)
For more details check out this link.
有关更多详细信息,请查看此链接。
回答by huojun
As suggested by lgautier, it can be done with pandas2ri.
正如 lgautier 所建议的那样,可以使用pandas2ri.
Here is sample code for convert rpy dataframe (rdf) to pandas dataframe (pd_df):
以下是将 rpy 数据帧 ( rdf)转换为 Pandas 数据帧 ( ) 的示例代码pd_df:
from rpy2.robjects import pandas2ri
pd_df = pandas2ri.ri2py_dataframe(rdf)
回答by ely
Given your import, it appears it is:
鉴于您的导入,它似乎是:
com.convert_robj(rdf)
For example,
例如,
In [480]: dfrm
Out[480]:
A B C
0 0.454459 49.916767 1
1 0.943284 50.878174 1
2 0.974856 50.335679 2
3 0.776600 50.782104 1
4 0.553895 50.084505 1
5 0.514018 50.719019 2
6 0.915413 50.513962 0
7 0.771571 49.859855 2
8 0.068619 49.409657 0
9 0.728141 50.945174 2
10 0.388115 47.879653 1
11 0.960172 49.680258 0
12 0.015216 50.067968 0
13 0.495024 50.286287 1
14 0.565954 49.909771 1
15 0.992279 49.009696 1
16 0.179934 49.554256 0
17 0.521243 47.854791 0
18 0.551241 51.076262 1
19 0.713271 49.418503 0
20 0.801716 50.660304 1
In [481]: rdfrm = com.convert_to_r_dataframe(dfrm)
In [482]: rdfrm
Out[482]:
<DataFrame - Python:0x14905cf8 / R:0x1600ee98>
[FloatVector, FloatVector, IntVector]
A: <class 'rpy2.robjects.vectors.FloatVector'>
<FloatVector - Python:0xf9d0b00 / R:0x140e2620>
[0.454459, 0.943284, 0.974856, ..., 0.551241, 0.713271, 0.801716]
B: <class 'rpy2.robjects.vectors.FloatVector'>
<FloatVector - Python:0xf9d0878 / R:0x125aa240>
[49.916767, 50.878174, 50.335679, ..., 51.076262, 49.418503, 50.660304]
C: <class 'rpy2.robjects.vectors.IntVector'>
<IntVector - Python:0x11fceef0 / R:0x13f0d918>
[ 1, 1, 2, ..., 1, 0, 1]
In [483]: com.convert_robj(rdfrm)
Out[483]:
A B C
0 0.454459 49.916767 1
1 0.943284 50.878174 1
2 0.974856 50.335679 2
3 0.776600 50.782104 1
4 0.553895 50.084505 1
5 0.514018 50.719019 2
6 0.915413 50.513962 0
7 0.771571 49.859855 2
8 0.068619 49.409657 0
9 0.728141 50.945174 2
10 0.388115 47.879653 1
11 0.960172 49.680258 0
12 0.015216 50.067968 0
13 0.495024 50.286287 1
14 0.565954 49.909771 1
15 0.992279 49.009696 1
16 0.179934 49.554256 0
17 0.521243 47.854791 0
18 0.551241 51.076262 1
19 0.713271 49.418503 0
20 0.801716 50.660304 1
With docs:
使用文档:
In [475]: com.convert_robj?
Type: function
String Form:<function convert_robj at 0x13e85848>
File: /mnt/epd/7.3-2_pandas0.12/lib/python2.7/site-packages/pandas/rpy/common.py
Definition: com.convert_robj(obj, use_pandas=True)
Docstring:
Convert rpy2 object to a pandas-friendly form
Parameters
----------
obj : rpy2 object
Returns
-------
Non-rpy data structure, mix of NumPy and pandas objects
回答by Rock Pereira
Use pandas to read an rpy2 dataframe, r_df. It will avoid the deprecation warning "FutureWarning: from_items is deprecated. Use DataFrame.from_dict(dict(items), ...) instead"
使用 pandas 读取 rpy2 数据帧,r_df. 它将避免弃用警告“FutureWarning: from_items 已弃用。使用 DataFrame.from_dict(dict(items), ...) 代替”
type(r_df)is "rpy2.robjects.vectors.DataFrame".type(pd_df)is "pandas.core.frame.DataFrame"
type(r_df)是“rpy2.robjects.vectors.DataFrame”。type(pd_df)是“pandas.core.frame.DataFrame”
pd_df = pd.DataFrame.from_dict({ key : np.asarray(r_df.rx2(key)) for key in r_df.names })
pd_df = pd.DataFrame.from_dict({ key : np.asarray(r_df.rx2(key)) for key in r_df.names })

