I am using R off and on as a "backend" to Python and thus need to occassionaly import dataframes from R into Python; but I can't figure out how to import an R data.frame
as a Pandas DataFrame
.
For example if I create a dataframe in R
rdf = data.frame(a=c(2, 3, 5), b=c("aa", "bb", "cc"), c=c(TRUE, FALSE, TRUE))
and then pull it into Python using rmagic
with
%Rpull -d rdf
I get
array([(2.0, 1, 1), (3.0, 2, 0), (5.0, 3, 1)],
dtype=[('a', '<f8'), ('b', '<i4'), ('c', '<i4')])
I don't know what this is, and it's certainly not the
pd.DataFrame({'a': [2, 3, 5], 'b': ['aa', 'bb', 'cc'], 'c': [True, False, True]})
that I would expect.
The only thing that comes close to working for me is to use use a file to transfer the dataframe by writing in R
write.csv(data.frame(a=c(2, 3, 5), b=c("aa", "bb", "cc"), c=c(TRUE, FALSE, TRUE)), file="TEST.csv")
and then reading in Python
pd.read_csv("TEST.csv")
though even this approach produces an additional column: "Unnamed: 0".
What is the idiom for importing an R dataframe into Python as a Pandas dataframe?