gpt4 book ai didi

json - 如何将复杂的 JSON 数据转换为单个数据帧?

转载 作者:行者123 更新时间:2023-12-03 07:34:01 26 4
gpt4 key购买 nike

我不知道如何将我的 JSON 数据正确转换为有用的数据帧。这是一些示例数据,显示了我的数据结构:

{
"data":[
{"track":[
{"time":"2015","midpoint":{"x":6,"y":8},"realworld":{"x":1,"y":3},"coordinate":{"x":16,"y":38}},
{"time":"2015","midpoint":{"x":6,"y":8},"realworld":{"x":1,"y":3},"coordinate":{"x":16,"y":37}},
{"time":"2016","midpoint":{"x":6,"y":9},"realworld":{"x":2,"y":3},"coordinate":{"x":16,"y":38}}
]},
{"track":[
{"time":"2015","midpoint":{"x":5,"y":9},"realworld":{"x":-1,"y":3},"coordinate":{"x":16,"y":38}},
{"time":"2015","midpoint":{"x":5,"y":9},"realworld":{"x":-1,"y":3},"coordinate":{"x":16,"y":38}},
{"time":"2016","midpoint":{"x":5,"y":9},"realworld":{"x":-1,"y":3},"coordinate":{"x":16,"y":38}},
{"time":"2015","midpoint":{"x":3,"y":15},"realworld":{"x":-9,"y":2},"coordinate":{"x":17,"y":38}}
]},
{"track":[
{"time":"2015","midpoint":{"x":6,"y":7},"realworld":{"x":-2,"y":3},"coordinate":{"x":16,"y":39}}
]}]}

我有很多轨道,我希望数据集如下所示:
track   time   midpoint   realworld   coordinate 
1
1
1
2
2
2
2
3

到目前为止,我有这个:
json_file <- "testdata.json"
data <- fromJSON(json_file)
data2 <- list.stack(data, fill=TRUE)

现在它是这样的:

data output

我怎样才能以正确的格式得到这个?

最佳答案

添加 flatten = TRUE使用 fromJSON 读取时的参数.这将为您提供一个嵌套列表,其中最深层次是三个数据框的列表。使用:

library(jsonlite)
# read the json
jsondata <- fromJSON(txt, flatten = TRUE)

# bind the dataframes in the nested 'track' list together
dat <- do.call(rbind, jsondata$data$track)

# add a track variable
dat$track <- rep(1:length(jsondata$data$track), sapply(jsondata$data$track, nrow))

给出:
> dat
time midpoint.x midpoint.y realworld.x realworld.y coordinate.x coordinate.y track
1 2015 6 8 1 3 16 38 1
2 2015 6 8 1 3 16 37 1
3 2016 6 9 2 3 16 38 1
4 2015 5 9 -1 3 16 38 2
5 2015 5 9 -1 3 16 38 2
6 2016 5 9 -1 3 16 38 2
7 2015 3 15 -9 2 17 38 2
8 2015 6 7 -2 3 16 39 3

另一种更短的方法是使用 jsonlite结合 rbindlist来自 data.table包裹:
library(jsonlite)
library(data.table)
# read the json
jsondata <- fromJSON(txt, flatten = TRUE)
# bind the dataframes in the nested 'track' list together
# and include an id-column at the same time
dat <- rbindlist(jsondata$data$track, idcol = 'track')

或与 bind_rows来自 dplyr以类似的方式打包:
library(dplyr)
dat <- bind_rows(jsondata$data$track, .id = 'track')

使用数据:
txt <- '{
"data":[
{"track":[
{"time":"2015","midpoint":{"x":6,"y":8},"realworld":{"x":1,"y":3},"coordinate":{"x":16,"y":38}},
{"time":"2015","midpoint":{"x":6,"y":8},"realworld":{"x":1,"y":3},"coordinate":{"x":16,"y":37}},
{"time":"2016","midpoint":{"x":6,"y":9},"realworld":{"x":2,"y":3},"coordinate":{"x":16,"y":38}}
]},
{"track":[
{"time":"2015","midpoint":{"x":5,"y":9},"realworld":{"x":-1,"y":3},"coordinate":{"x":16,"y":38}},
{"time":"2015","midpoint":{"x":5,"y":9},"realworld":{"x":-1,"y":3},"coordinate":{"x":16,"y":38}},
{"time":"2016","midpoint":{"x":5,"y":9},"realworld":{"x":-1,"y":3},"coordinate":{"x":16,"y":38}},
{"time":"2015","midpoint":{"x":3,"y":15},"realworld":{"x":-9,"y":2},"coordinate":{"x":17,"y":38}}
]},
{"track":[
{"time":"2015","midpoint":{"x":6,"y":7},"realworld":{"x":-2,"y":3},"coordinate":{"x":16,"y":39}}
]}]}'

关于json - 如何将复杂的 JSON 数据转换为单个数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39747811/

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