I use R off and is included as a "backend" for Python, and therefore, it is necessary to import dataframes from R from Python from time to time; but I can't figure out how to import R data.frameas 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 in python using rmagicwith
%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 it is, and it certainly doesn’t
pd.DataFrame({'a': [2, 3, 5], 'b': ['aa', 'bb', 'cc'], 'c': [True, False, True]})
which I would expect.
The only thing that suits me to work is to use a file to transfer data, writing to 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")
although even this approach creates an additional column: "Unnamed: 0".
What is the idiom for importing an R data frame in Python as a Pandas dataframe?