【问题标题】:Parsing JSON from a specific column in a CSV file从 CSV 文件中的特定列解析 JSON
【发布时间】:2019-05-03 15:23:20
【问题描述】:

我目前正在使用 Kaggle (https://www.kaggle.com/tmdb/tmdb-movie-metadata) 上的 TMDB_5000_movies.csv 数据集,在将包含 JSON 对象的列转换为规范化数据框时需要一些帮助。

例如,如果一部电影有多个与之相关的类型,我希望我的数据集是这样的:

预算 |类型_1 |类型_2 |等等

100000000 |行动 |冒险 | ...

我只是根据与它相关的流派最多的电影来确定genre_x 列的数量。我查看了 RJSON 和 JSONLITE 之类的包,似乎无法让它们与包含在 CSV 文件中的 JSON 一起使用,而只是 JSON 本身。提前致谢!

【问题讨论】:

    标签: r json csv


    【解决方案1】:
    library(tidyverse)
    
    read_csv("~/Downloads/tmdb_5000_credits.csv") %>%
      mutate(
        cast = map(cast, jsonlite::fromJSON),
        crew = map(crew, jsonlite::fromJSON)
      ) -> xdf
    

    制作一个长的“cast”数据框:

    map_df(1:nrow(xdf), ~{
      cast <- xdf$cast[[.x]]
      cast$movie_id <- xdf$movie_id[.x]
      cast$title <- xdf$title[.x]
      tbl_df(cast)
    }) -> cast_df
    
    cast_df
    ## # A tibble: 106,300 x 9
    ##    cast_id character   credit_id  gender    id name   order movie_id title
    ##      <int> <chr>       <chr>       <int> <int> <chr>  <int>    <int> <chr>
    ##  1     242 Jake Sully  5602a8a7c…      2 65731 Sam W…     0    19995 Avat…
    ##  2       3 Neytiri     52fe48009…      1  8691 Zoe S…     1    19995 Avat…
    ##  3      25 Dr. Grace … 52fe48009…      1 10205 Sigou…     2    19995 Avat…
    ##  4       4 Col. Quari… 52fe48009…      2 32747 Steph…     3    19995 Avat…
    ##  5       5 Trudy Chac… 52fe48009…      1 17647 Miche…     4    19995 Avat…
    ##  6       8 Selfridge   52fe48009…      2  1771 Giova…     5    19995 Avat…
    ##  7       7 Norm Spell… 52fe48009…      2 59231 Joel …     6    19995 Avat…
    ##  8       9 Moat        52fe48009…      1 30485 CCH P…     7    19995 Avat…
    ##  9      11 Eytukan     52fe48009…      2 15853 Wes S…     8    19995 Avat…
    ## 10      10 Tsu'Tey     52fe48009…      2 10964 Laz A…     9    19995 Avat…
    ## # ... with 106,290 more rows
    

    制作一个长的“船员”数据框:

    map_df(1:nrow(xdf), ~{
      crew <- xdf$crew[[.x]]
      crew$movie_id <- xdf$movie_id[.x]
      crew$title <- xdf$title[.x]
      tbl_df(crew)
    }) -> crew_df
    
    crew_df
    ## # A tibble: 129,609 x 8
    ##    credit_id     department gender    id job        name    movie_id title
    ##    <chr>         <chr>       <int> <int> <chr>      <chr>      <int> <chr>
    ##  1 52fe48009251… Editing         0  1721 Editor     Stephe…    19995 Avat…
    ##  2 539c47ecc3a3… Art             2   496 Productio… Rick C…    19995 Avat…
    ##  3 54491c89c3a3… Sound           0   900 Sound Des… Christ…    19995 Avat…
    ##  4 54491cb70e0a… Sound           0   900 Supervisi… Christ…    19995 Avat…
    ##  5 539c4a4cc3a3… Production      1  1262 Casting    Mali F…    19995 Avat…
    ##  6 5544ee3b9251… Sound           2  1729 Original … James …    19995 Avat…
    ##  7 52fe48009251… Directing       2  2710 Director   James …    19995 Avat…
    ##  8 52fe48009251… Writing         2  2710 Writer     James …    19995 Avat…
    ##  9 52fe48009251… Editing         2  2710 Editor     James …    19995 Avat…
    ## 10 52fe48009251… Production      2  2710 Producer   James …    19995 Avat…
    ## # ... with 129,599 more rows
    

    现在,您可以使用任何 tidyverse 和非 tidyverse 方法来分组、汇总、计数等,然后重新加入更紧凑的结构。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 1970-01-01
      • 2014-10-09
      • 2017-05-14
      • 1970-01-01
      • 2022-01-21
      • 2016-12-24
      相关资源
      最近更新 更多