【问题标题】:How to export a list of dataframes from R to Python?如何将数据框列表从 R 导出到 Python?
【发布时间】:2019-04-17 00:06:50
【问题描述】:

我目前正在使用 R 中的功能性 MRI 数据,但我需要将其导入 Python 以进行更快的分析。我怎样才能有效地做到这一点?

我目前在 R 中有 198135 个数据帧的列表。它们都有 5 个变量和 84 个大脑区域之间连接的观察结果。我需要在 Python 中显示相同的 198135 个数据帧,以便在那里运行一些特定的分析(具有与 R 中相同的结构:一个对象分别包含所有数据帧)。

最初我尝试从 R 导出一个 .RDS 文件,然后使用“pyreadr”将其导入 Python,但每次尝试使用“pyreadr.read_r()”函数都会得到空对象。

我的另一种方法是将 R 列表的每个数据帧保存为单独的 .csv 文件,然后将它们导入 Python。这样我就可以得到我想要的(我用 100 个数据帧尝试了它,只是为了尝试代码)。这种方法的问题是效率低下且速度慢。

我找到了几个类似问题的答案,但其中大多数是合并所有数据帧并将其作为唯一的 .csv 加载到 Python 中,这不是我需要的解决方案。

在不改变我提到的数据结构的情况下,有没有更有效的方法来完成这个过程?

感谢您的帮助!

# This is the code in R for an example

a <- as.data.frame(cbind(c(1:3), c(1:3), c(4:6), c(7:9)))
b <- as.data.frame(cbind(c(11:13), c(21:23), c(64:66), c(77:79)))
c <- as.data.frame(cbind(c(31:33), c(61:63), c(34:36), c(57:59)))
d <- as.data.frame(cbind(c(12:14), c(13:15), c(54:56), c(67:69)))
e <- as.data.frame(cbind(c(31:33), c(51:53), c(54:56), c(37:39)))

somelist_of_df <- list(a,b,c,d,e)


saveRDS(somelist_of_df, "somefile.rds") 
## This is the function I used from pyreadr in Python


import pyreadr

results = pyreadr.read_r('/somepath/somefile.rds')


【问题讨论】:

  • 您可以查看feather package,它适用于 python 和 R,并且比 csv 快得多并保留列类型。

标签: python r dataframe


【解决方案1】:

好吧,感谢其他答案的帮助,但这并不是我想要的(我只想导出一个包含数据框列表的文件,然后将一个文件加载到 Python,保留相同的结构)。要使用羽毛,您必须分解其中所有数据帧中的列表,就像保存单独的 .csv 文件,然后将它们中的每一个加载到 Python(或 R)中。无论如何,必须说它比.csv的方法快得多。

我将成功使用的代码留在单独的答案中,也许它对其他人有用,因为我使用了一个简单的循环将数据帧作为列表加载到 Python 中:

## Exporting a list of dataframes from R to .feather files

library(feather) #required package

a <- as.data.frame(cbind(c(1:3), c(1:3), c(4:6), c(7:9))) #Example DFs
b <- as.data.frame(cbind(c(11:13), c(21:23), c(64:66), c(77:79)))
c <- as.data.frame(cbind(c(31:33), c(61:63), c(34:36), c(57:59)))
d <- as.data.frame(cbind(c(12:14), c(13:15), c(54:56), c(67:69)))
e <- as.data.frame(cbind(c(31:33), c(51:53), c(54:56), c(37:39)))

somelist_of_df <- list(a,b,c,d,e) 

## With sapply you loop over the list for creating the .feather files

sapply(seq_along(1:length(somelist_of_df)), 
       function(i) write_feather(somelist_of_df[[i]], 
                                 paste0("/your/directory/","DF",i,".feather")))

(仅使用 MacBook Air,上面的代码运行 198135 个 DF 的列表不到 5 秒)

## Importing .feather files into a list of DFs in Python

import os
import feather

os.chdir('/your/directory')

directory = '/your/directory'

py_list_of_DFs = []

for filename in os.listdir(directory):
    DF = feather.read_dataframe(filename)
    py_list_of_DFs.append(DF)

(此代码为我完成了工作,除了它有点慢,完成 198135 个 DF 的任务需要 12 分钟)

我希望这对某人有用。

【讨论】:

    【解决方案2】:

    This package 可能你会感兴趣

    【讨论】:

      【解决方案3】:

      Pandas 还实现了直接读取 .feather 文件的方式:

      pd.read_feather()

      【讨论】:

        【解决方案4】:

        Pyreadr 目前无法读取 R 列表,因此您需要单独保存数据帧,还需要保存到 RDA 文件,以便您可以在一个文件中托管多个数据帧:

        # first construct a list with the names of dataframes you want to save
        # instead of the dataframes themselves
        somelist_of_df <- list("a", "b", "c", "d", "e")
        do.call("save",  c(somelist_of_df, file="somefile.rda"))
        

        here所述的任何其他变体。

        然后就可以用python读取文件了:

        import pyreadr
        
        results = pyreadr.read_r('/somepath/somefile.rda')
        

        优点是只有一个文件包含所有数据帧。

        【讨论】:

          【解决方案5】:

          由于声誉,我无法在@crlagos0 答案中发表评论。我想补充几点:

          seq_along(list_of_things)就够了,不用R里的seq_along(lenght(1:list_of_things))。另外,我想指出的是,在R 中读写羽毛文件的官方包称为arrow,您可以找到它的文档here。在python 中是pyarrow

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-12-29
            • 1970-01-01
            • 2012-09-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多