将json格式的列转换为新的数据框 [英] converting a column in json format into a new data frame

查看:67
本文介绍了将json格式的列转换为新的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 csv文件,其中一列为 json格式.

I have a csv file and one of the column is in json format.

json格式的特定列如下所示:

that particular column in json format looks like this:

{"title":" ","body":" ","url":"thedailygreen print this healthy eating eat safe Dirty Dozen Foods page all"}

我已经在R中使用read.csv读取了此文件.现在,如何从此列创建一个新的数据框,该字段的名称应为title,body和url.

I have read this file using read.csv in R. Now, how to I create a new data frame from this column which should have field names as title, body and url.

推荐答案

您可以使用包RJSONIO解析列值,例如:

You can use package RJSONIO to parse the column values, e.g. :

library(RJSONIO)

# create an example data.frame with a json column
cell1 <- '{"title":"A","body":"X","url":"http://url1.x"}'
cell2 <- '{"title":"B","body":"Y","url":"http://url2.y"}'
cell3 <- '{"title":"C","body":"Z","url":"http://url3.z"}'

df <- data.frame(jsoncol = c(cell1,cell2,cell3),stringsAsFactors=F)

# parse json and create a data.frame
res <- do.call(rbind.data.frame,
               lapply(df$jsoncol, FUN=function(x){ as.list(fromJSON(x))}))

> res
   title body           url
     A    X   http://url1.x
     B    Y   http://url2.y
     C    Z   http://url3.z

: 上面的代码假定所有单元格仅包含标题,正文和url.如果json单元中可以有其他属性,请改用以下代码:

N.B. : the code above assumes all the cells contains title, body and url only. If there can be other properties in the json cells, use this code instead :

vals <- lapply(df$jsoncol,fromJSON)
res <- do.call(rbind, lapply(vals,FUN=function(v){ data.frame(title=v['title'],
                                                              body =v['body'],
                                                              url  =v['url']) }))

编辑(根据评论):

我已使用以下代码读取文件:

I've read the file using the following code :

df <- read.table(file="c:\\sample.tsv", 
                 header=T, sep="\t", colClasses="character")

然后使用以下代码进行解析:

then parsed using this code :

# define a simple function to turn NULL to NA
naIfnull <- function(x){if(!is.null(x)) x else NA}

vals <- lapply(df$boilerplate,fromJSON)
res <- do.call(rbind, 
               lapply(vals,FUN=function(v){ v <- as.list(v)
                                            data.frame(title=naIfnull(v$title),
                                                       body =naIfnull(v$body),
                                                       url  =naIfnull(v$url)) }))

这篇关于将json格式的列转换为新的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆