gpt4 book ai didi

python - 如何从嵌套列表中提取特定项目并附加到新列?

转载 作者:行者123 更新时间:2023-12-04 16:24:05 24 4
gpt4 key购买 nike

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

虚拟数据:

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 中的数据是节点,用户名是提及,因此是边。如果有其他方法可以做到这一点,而不是从嵌套列表中提取用户名,这也可能是一种解决方案。

提前感谢您的帮助! :)

最佳答案

如果我们知道嵌套层次,可以使用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

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

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

如果我们想粘贴它们,请使用

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"

关于python - 如何从嵌套列表中提取特定项目并附加到新列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68749290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com