将使用 Feather 存储的 Pandas 数据帧读入 R

Reading Pandas data frame stored with Feather into R

我只是试图将 Pandas 数据帧读入 R 并使用 Feather 存储到磁盘。读入数据框后,我检查了对象的类型,结果没有看到 'data.frame',而是 'tbl_df' 'tbl' 'data.frame'。知道这里发生了什么吗?

相关代码很简单: contact_records_df <- read_feather('contact_records.feather') class(contact_records_df)

它只是作为 tibble 引入的,它或多或少是来自 tidyverse 世界的 'enhanced' 数据帧。您可以查看文档 here

您可以将它们与数据框互换使用。我偶尔注意到,尤其是对于空间函数,tibbles 会导致某些东西死亡,因此您有时必须将它们转换回数据框。

library(tibble)

x1 <- c(1, 2, 3, 4)
x2 <- c("one", "two", "three", "four")

example_df <- data.frame(x1,x2)
example_tibble <- tibble(x1,x2)

如果您使用 str 查看它们中的两个,您会发现它们基本相同,除了 tibbles 不会自动将字符串转换为因子(以及其他)。

> str(example_df)
'data.frame':   4 obs. of  2 variables:
 $ x1: num  1 2 3 4
 $ x2: Factor w/ 4 levels "four","one","three",..: 2 4 3 1
> str(example_tibble)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   4 obs. of  2 variables:
 $ x1: num  1 2 3 4
 $ x2: chr  "one" "two" "three" "four"

此外,它仍然是一个数据框,但它有一些更具体的 类

> is.data.frame(example_tibble)
[1] TRUE