Python Pandas转R数据框 [英] Python Pandas to R dataframe
问题描述
我打算将Python pandas数据框转换为R中的数据框. 我发现了几个库来解决这个问题
I am going to convert Python pandas dataframe to dataframe in R. I found out few libraries for this problem
http://pandas.pydata.org/pandas-docs/stable/r_interface.html
这是rpy2
但是我找不到保存或转移到R的方法.
But I couldn't find the methods for saving or transfer it to R.
首先我尝试了"to_csv"
Firstly I tried "to_csv"
df_R = com.convert_to_r_dataframe(df_total)
df_R.to_csv(direc+"/qap/detail_summary_R/"+"distance_"+str(gp_num)+".csv",sep = ",")
但这给我一个错误
"AttributeError: 'DataFrame' object has no attribute 'to_csv' "
所以我试图查看它的数据类型 是
So I tried to see its data type it was
<class 'rpy2.robjects.vectors.DataFrame'>
如何将这种类型的对象保存到csv文件或转移到R?
how could I save this type object to csv file or transfer to R?
推荐答案
The recent documentation https://rpy2.github.io/doc/v3.2.x/html/generated_rst/pandas.html has a section about interacting with pandas
.
否则,类型为rpy2.robjects.vectors.DataFrame
的对象具有方法to_csvfile
,而不是方法to_csv
:
Otherwise objects of type rpy2.robjects.vectors.DataFrame
have a method to_csvfile
, not to_csv
:
https ://rpy2.github.io/doc/v3.2.x/html/vector.html#rpy2.robjects.vectors.DataFrame.to_csvfile
如果要在Python和R之间传递数据,则有比写入和读取CSV文件更有效的方法.尝试转换系统:
If wanting to pass data between Python and R, there are more efficient ways than writing and reading CSV files. Try the conversion system:
from rpy2.robjects import pandas2ri
pandas2ri.activate()
from rpy2.robjects.packages import importr
base = importr('base')
# call an R function on a Pandas DataFrame
base.summary(my_pandas_dataframe)
这篇关于Python Pandas转R数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!