Python 如何通过网状结构在R中使用pandas编写csv?

Python 如何通过网状结构在R中使用pandas编写csv?,python,r,pandas,reticulate,Python,R,Pandas,Reticulate,我可以使用Networkite在rstudio中读取csv。但是我不能把它写回去 > 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

我可以使用Networkite在rstudio中读取csv。但是我不能把它写回去

> 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"      
>库(网状)
>.file nrow中的熊猫(in.file)
[1] 504
>类(在.file中)
[1] “数据帧”
>in.file类(in.file)
[1] “pandas.core.frame.DataFrame”“pandas.core.generic.NDFrame”“pandas.core.base.PandaObject”“pandas.core.base.StringMixin”
[5] “pandas.core.accessor.dirnamemixin”“pandas.core.base.SelectionMixin”“python.builtin.object”

熊猫数据帧对象具有
to_csv
属性,但您的
in.file
对象在读入时自动转换为R data.frame,因此它没有这些属性。为了使用Python方法将数据帧写回CSV,需要首先使用
r\u to\u py()
函数将其转换为Python对象:

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

infle\u-converted这就是R和Python对象模型的不同之处。在R中,像
write.csv()
这样的方法在对象上运行。但在Python中,对象可以具有使用其父对象的可调用属性或属性,如

因此,只需将方法从pandas库调整到数据帧本身:

in.file$to_csv("/path/to/output.csv")
事实上,许多是数据帧属性:

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()