I'm using the R package PythonInR
, and wanted to convert the pandas dataframe to R dataframe using function pyGet()
, but I got the error below:
Error in as.data.frame.default(xi, optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""PythonObject"" to a data.frame
The text.csv file only contains two columns, name(string) and value(int). The code is as below:
library(PythonInR)
pyConnect()
pyIsConnected()
pyVersion()
pyOptions("usePandas", TRUE)
pyImport("pandas", as="pd")
test_code <-'py_df = pd.read_csv("test.csv")'
pyExec(code = test_code)
r_df <- pyGet("py_df")
And the output of the code is:
library(PythonInR)
pyConnect()
R is already connected to Python!
pyIsConnected()
[1] TRUE
pyVersion()
[1] "3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)]"
pyOptions("usePandas", TRUE)
pyImport("pandas", as="pd")
test_code <- 'py_df = pd.read_csv("test.csv")'
pyExec(code = test_code)
r_df <- pyGet("py_df")
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""PythonObject"" to a data.frame
Can anyone help to suggest how to convert a Pandas data frame to R data.frame using PythonInR
?
read.table
rather than pursuing such a round about method? Or just stay in Python. There doesn't seem to be any case for synergy here. – Steven Rumbalski