2

I can read a csv in rstudio using reticulate. However I am not able to write it back.

> library(reticulate)
> pandas <- import("pandas")
> in.file <- pandas$read_csv(file.path(getwd(),"in.csv"))
> nrow(in.file)
[1] 504
> class(in.file)
[1] "data.frame"
> in.file<-r_to_py(in.file)
> class(in.file)
[1] "pandas.core.frame.DataFrame"        "pandas.core.generic.NDFrame"        "pandas.core.base.PandasObject"      "pandas.core.base.StringMixin"      
[5] "pandas.core.accessor.DirNamesMixin" "pandas.core.base.SelectionMixin"    "python.builtin.object"      
itthrill
  • 1,241
  • 2
  • 17
  • 36

2 Answers2

2

This is where the R and Python object models differ. In R methods like write.csv() are run on objects. But in Python objects can have callable properties or attributes that use its parent object like DataFrame.to_csv().

So simply adjust method from pandas library to the data frame itself:

in.file$to_csv("/path/to/output.csv")

In fact many of the I/O methods are data frame properties:

in.file$to_excel("/path/to/output.xlsx", excel_writer)
in.file$to_sql(engine, "table_name")
in.file$to_hdf("hdf5_store", "table_name")

# OUTPUTS TO STRING
json_str = in.file$to_json()
html_str = in.file$to_html()
Parfait
  • 104,375
  • 17
  • 94
  • 125
1

A pandas dataframe object has a to_csv attribute, but your in.file object was automatically converted to an R data.frame when being read in, so it does not have those attributes. In order to write the data frame back to a CSV using the Python method, you need to convert it to a Python object first using the r_to_py() function:

infile_converted <- r_to_py(in.file)
infile_converted$to_csv(file.path(getwd(), 'out.csv'))

The other option would just be to use the native R function write.csv().

qdread
  • 3,389
  • 19
  • 36