Python支持R数据帧

我将把Python的dataframe转换为R中的dataframe。
我发现很少有库可以解决这个问题

http://pandas.pydata.org/pandas-docs/stable/r_interface.html

哪个是rpy2

但是我找不到保存或转移到R的方法

首先,我试着“去”

df\u R=com.将\u转换为\u R\u数据帧(df\u总计)
df_R.to_csv(direc+”/qap/detail_summary_R/“+”距离+str(gp_num)+“.csv”,sep=“,”)

但这给了我一个错误

“AttributeError:'DataFrame'对象没有“to_csv”属性”

所以我试着查看它的数据类型
是的

<类“rpy2.robjects.vectors.DataFrame”>

如何将此类型对象保存到csv文件或传输到R

如果标准的基于文本的格式(csv)太慢或太庞大,我建议使用feather,这是一种基于ApacheArrow的序列化格式。它是由RStudio/ggplot2/etc(Hadley Wickham)和pandas(Wes McKinney)的创建者明确开发的,用于Python和R之间的性能和互操作性(参见此处)

您需要使用0.20.0+,pip安装feather format,然后您可以使用to_feather/read_feather操作作为to_csv/read_csv的插入式替换:

df\u R.to\u feather('filename.feather'))
df_R=pd.read_feather('filename.feather'))

R等价物(使用软件包feather)为

df<-feather::read_feather('filename.feather'))
feather::write_feather(df,'filename.feather')

除了一些小的调整(例如,您无法在feather中保存自定义数据帧索引,因此您需要首先调用df.reset_index()),这是一个快速而简单的替换csvpickle等的方法

发表评论