【问题标题】:How to extract specific items from a nested list and append to new column?如何从嵌套列表中提取特定项目并附加到新列?
【发布时间】:2021-10-15 08:16:27
【问题描述】:

我有一个数据框,其中有一列包含嵌套列表。我正在努力从这些嵌套列表中提取用户名(我对此很陌生)。

虚拟数据:

myNestedList <- list("1" = list('username' = "test",
                              "uninteresting data" = "uninteresting content"),
                     "2" = list('username' = "test2",
                                "uninteresting data" = "uninteresting content"))
Column1 <- c("A","B","C")
column2 <- c("a","b","c")
mydf <- data.frame(Column1, column2)
mydf$nestedlist <- list(myNestedList)

我想提取每一行的所有用户名并将它们附加到一个新列中,如果一行有多个用户名,则第二个/第三个/第 n 个用户名应该附加一个分隔符 ", ”。 我尝试过类似sapply(mydf$nestedlist, [[, 1) 之类的东西,但这只是给了我整个列“nestedlist”的一个列表。

对于上下文:我正在尝试构建一个有向图,以便在 Networkx 或 Gephi 中进一步使用。 column1 中的数据是节点,用户名是提及,因此是边。如果有其他方法可以做到这一点,而不是从嵌套列表中提取用户名,这也可能是一种解决方案。

提前感谢您的帮助! :)

【问题讨论】:

    标签: python r dataframe networkx nested-lists


    【解决方案1】:

    如果我们知道嵌套层次,可以使用map_depth

    library(purrr)
     mydf$username <- map_depth(mydf$nestedlist, 2, pluck, "username")
    

    -输出

    > mydf
      Column1 column2                                                nestedlist    username
    1       A       a test, uninteresting content, test2, uninteresting content test, test2
    2       B       b test, uninteresting content, test2, uninteresting content test, test2
    3       C       c test, uninteresting content, test2, uninteresting content test, test2
    

    或者如果不知道,则使用带有condition 检查的递归函数来查找“用户名”

    library(rrapply)
    mydf$username <- rrapply(mydf$nestedlist,  
        condition = function(x, .xname) .xname %in% 'username', how = 'prune')
    > mydf
      Column1 column2                                                nestedlist    username
    1       A       a test, uninteresting content, test2, uninteresting content test, test2
    2       B       b test, uninteresting content, test2, uninteresting content test, test2
    3       C       c test, uninteresting content, test2, uninteresting content test, test2
    

    如果我们想paste他们,请使用

    library(stringr)
    library(dplyr)
    mydf$username <- rrapply(mydf$nestedlist,  
        condition = function(x, .xname) .xname %in% 'username',
              how = 'bind') %>% 
            invoke(str_c, sep=", ", .)
     mydf
      Column1 column2                                                nestedlist    username
    1       A       a test, uninteresting content, test2, uninteresting content test, test2
    2       B       b test, uninteresting content, test2, uninteresting content test, test2
    3       C       c test, uninteresting content, test2, uninteresting content test, test2
    

    -结构

    > str(mydf)
    'data.frame':   3 obs. of  4 variables:
     $ Column1   : chr  "A" "B" "C"
     $ column2   : chr  "a" "b" "c"
     $ nestedlist:List of 3
      ..$ :List of 2
      .. ..$ 1:List of 2
      .. .. ..$ username          : chr "test"
      .. .. ..$ uninteresting data: chr "uninteresting content"
      .. ..$ 2:List of 2
      .. .. ..$ username          : chr "test2"
      .. .. ..$ uninteresting data: chr "uninteresting content"
      ..$ :List of 2
      .. ..$ 1:List of 2
      .. .. ..$ username          : chr "test"
      .. .. ..$ uninteresting data: chr "uninteresting content"
      .. ..$ 2:List of 2
      .. .. ..$ username          : chr "test2"
      .. .. ..$ uninteresting data: chr "uninteresting content"
      ..$ :List of 2
      .. ..$ 1:List of 2
      .. .. ..$ username          : chr "test"
      .. .. ..$ uninteresting data: chr "uninteresting content"
      .. ..$ 2:List of 2
      .. .. ..$ username          : chr "test2"
      .. .. ..$ uninteresting data: chr "uninteresting content"
     $ username  : chr  "test, test2" "test, test2" "test, test2"
    

    【讨论】:

    • 太好了,成功了!但是,使用您的第一个解决方案 (purrr),新列“用户名”中的结果也是列表。使用 mydf$username_clean &lt;- unlist(mydf$username) 会引发错误。有没有办法修改您的解决方案以使其不返回列表?
    • @Cold2Breath 如果元素不止一个,要不要paste
    • @Cold2Breath 我更新了帖子pasteing 元素以创建向量
    • 非常感谢您的帮助!由于某种原因,rrapply 不可用,即使我使用的是 R 3.9。但是,我能够使用正则表达式清除其余部分! :)
    • @Cold2Breath 你可以使用来自base Rrapply,但有一些修改。我用R 4.1.0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 2015-08-10
    相关资源
    最近更新 更多