How to convert Python Pandas Dataframe to R data.frame -
i'm using r package pythoninr
, , wanted convert pandas dataframe r dataframe using function pyget()
, got error below:
error in as.data.frame.default(xi, optional = true, stringsasfactors = stringsasfactors) : cannot coerce class ""pythonobject"" data.frame
the text.csv file contains 2 columns, name(string) , value(int). code 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 output of code is:
library(pythoninr) pyconnect() r connected 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"" data.frame
can suggest how convert pandas data frame r data.frame using pythoninr
?
you use json. pandas dataframe has to_json
method , can use rjson
r package read json string using fromjson
.
you may find this , this interesting. planning submit cran soon, need more testing.
Comments
Post a Comment