我可以使用rpy2将 pandas 数据框保存到.Rdata文件吗? [英] Can I use rpy2 to save a pandas dataframe to an .Rdata file?

查看:65
本文介绍了我可以使用rpy2将 pandas 数据框保存到.Rdata文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未使用过rpy2,但是我只是想知道是否可以使用它来将python对象(pandas DataFrame)保存在R可读文件中.我在这些环境之间移动对象时遇到了麻烦,主要是因为我正在使用Windows,并且数据源是Excel文件.是的,这种类型的单元格带有文本,包括逗号,换行符以及CSV无法正常处理的所有内容.

I never used rpy2 before, but I am just wondering if I could use it to save a python object (a pandas DataFrame) in an R-readable file. I am having trouble to move objects between these environments mainly because I'm using Windows and the data source is an Excel file. Yes, the kind that has cells with text including inverted commas, newlines, and all the stuff that CSV can't handle adequately.

我通常依靠XLConnectJars,但它似乎已损坏

I usually rely on XLConnectJars, but it seems to be broken

Installing package(s) into ‘C:/Program Files/R/library’
(as ‘lib’ is unspecified)
trying URL 'http://cran.csiro.au/bin/windows/contrib/2.15/XLConnectJars_0.2-4.zip'
Content type 'application/zip' length 16538311 bytes (15.8 Mb)
opened URL
downloaded 15.3 Mb

Warning in install.packages :
  downloaded length 16011264 != reported length 16538311

pandas可以正确读取,但是我需要使用R中的信息.

pandas reads it properly, but I need to use the information in R.

推荐答案

您可以使用rpy2执行此操作.将数据保存到熊猫中后,必须将其传输到R.此链接提供了Python Pandas和R data.frames之间的实验接口.从网站复制的代码示例:

You can use rpy2 to do this. Once you have the data in a panda, you have to transmit it to R. This link provides an experimental interface between Python Pandas and R data.frames. A code example copied from the website:

from pandas import DataFrame
import pandas.rpy.common as com

df = DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C':[7,8,9]},
                index=["one", "two", "three"])
r_dataframe = com.convert_to_r_dataframe(df)

print type(r_dataframe)
 <class 'rpy2.robjects.vectors.DataFrame'>

print r_dataframe
      A B C
one   1 4 7
two   2 5 8
three 3 6 9

这篇关于我可以使用rpy2将 pandas 数据框保存到.Rdata文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆