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
which is rpy2
But I couldn't find the methods for saving or transfer it to R.
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 = ",")
But it gives me an error
"AttributeError: 'DataFrame' object has no attribute 'to_csv' "
So I tried to see its data type it was
<class 'rpy2.robjects.vectors.DataFrame'>
how could I save this type object to csv file or transfer to R?
Once you have your data.frame you can save it using
write.table
or one of the wrappers of the latter, for examplewritee.csv
.In rpy2 :
If standard text-based formats (csv) are too slow or bulky, I'd recommend feather, a serialization format built on Apache Arrow. It was explicitly developed by the creators of RStudio/ggplot2/etc (Hadley Wickham) and pandas (Wes McKinney) for performance and interoperability between Python and R (see here).
You need pandas verson 0.20.0+,
pip install feather-format
, then you can use theto_feather
/read_feather
operations as drop-in replacements forto_csv
/read_csv
:The
R
equivalents (using the packagefeather
) areBesides some minor tweaks (e.g. you can't save custom DataFrame indexes in feather, so you'll need to call
df.reset_index()
first), this is a fast and easy drop-in replacement forcsv
,pickle
, etc.Objects of type
rpy2.robjects.vectors.DataFrame
have a methodto_csvfile
, notto_csv
: http://rpy.sourceforge.net/rpy2/doc-2.4/html/vector.html#rpy2.robjects.vectors.DataFrame.to_csvfileIf wanting to pass data between Python and R, there are more efficient ways than writing and reading CSV files. Try the conversion system: