【问题标题】:Json to dataframe (empty observations, different lengths)Json 到数据框(空观察,不同长度)
【发布时间】:2019-05-28 23:55:50
【问题描述】:

通过一个 API,我访问了 Json 格式的天气信息。我想将此数据转换为数据框。问题在于,并非每个日期-城市组合的 api 都会返回天气状况,因此有几行是空的。其次,并非所有返回的组合都给出了相同的天气方面。我的目标是将 Json 转换为数据框,其中空的行仍然显示在数据框中(当我取消列出它们时不会发生这种情况),并且天气的不同方面正确显示在具有 NA 值的正确变量下,如果该特定变量没有记录。我已经尝试将它们征集并将其放入数据框中,展平表格等(得到错误:参数意味着不同的行数:0、1)。我已经搜索过这个主题,但没有一个适合我的案例(或者可能是因为我没有经验,所以我错误地应用了它们),但欢迎每个提示!

输入看起来像这样:

reviewid    dateofwriting   lon         lat
98338143    28-02-11        11,41693611 22,3193039
58929813    18-03-10        -3,7037902  40,4167754
65945346    31-05-10        -3,188267   55,953252

输出如下所示(第二个观察返回 36 列,第三个返回 38 列。缺少第一个条目,因为当天没有观察并且未显示)

enter image description here

    [{},
    {"daily":
       [{"time":"2010-03-18",
       "summary":"Partly cloudy throughout the day.",
       "icon":"partly-cloudy-day",
       "sunriseTime":"2010-03-18 07:22:51",
       "sunsetTime":"2010-03-18 19:25:28",
       "moonPhase":0.08,
       "precipIntensity":0,
       "precipIntensityMax":0,
       "precipProbability":0,
       "temperatureHigh":63.14,  
       "temperatureHighTime":1268928000,
       "temperatureLow":45.16,
       "temperatureLowTime":1268971200,
       "apparentTemperatureHigh":63.14,
       "apparentTemperatureHighTime":1268928000,
       "apparentTemperatureLow":45.16,
       "apparentTemperatureLowTime":1268971200,
       "dewPoint":36.97,
       "humidity":0.58,
       "pressure":1025.96,
       "windSpeed":1.24,
       "windGust":7.87, 
       "windGustTime":1268866800,
       "windBearing":48,
       "cloudCover":0.54,
       "uvIndex":5,
       "uvIndexTime":1268913600,
       "visibility":6.19,
       "temperatureMin":43.97,
       "temperatureMinTime":"2010-03-18 07:00:00",
       "temperatureMax":63.14,
       "temperatureMaxTime":"2010-03-18 17:00:00",
       "apparentTemperatureMin":42.03,
       "apparentTemperatureMinTime":"2010-03-18 08:00:00",
       "apparentTemperatureMax":63.14,
       "apparentTemperatureMaxTime":"2010-03-18 17:00:00"}]},

    {"daily":
       [{"time":"2010-05-30 01:00:00",
       "summary":"Mostly cloudy until evening.",
       "icon":"partly-cloudy-day",
       "sunriseTime":"2010-05-30 05:38:39",
       "sunsetTime":"2010-05-30 22:44:55",
       "moonPhase":0.58,
       "precipIntensity":0.0038,
       "precipIntensityMax":0.0766,
       "precipIntensityMaxTime”:"2010-05-30 04:00:00",
       "precipProbability":1,
       "precipType":"rain", 
       "temperatureHigh":58.99,
       "temperatureHighTime":1275242400, 
       "temperatureLow":36.62,  
       "temperatureLowTime":1275278400, 
       "apparentTemperatureHigh":58.99, 
       "apparentTemperatureHighTime":1275242400, 
       "apparentTemperatureLow":36.62,
       "apparentTemperatureLowTime":1275278400,
       "dewPoint":43.61,
       "humidity":0.76,
       "pressure":1011.52,
       "windSpeed":4.65,
       "windGust":21.4,
       "windGustTime":1275224400,
       "windBearing":350,
       "cloudCover":0.61,
       "uvIndex":5,
       "uvIndexTime":1275213600,
       "visibility":5.85, 
       "temperatureMin":45.99,
       "temperatureMinTime":"2010-05-30 07:00:00",
       "temperatureMax":58.99,
       "temperatureMaxTime":"2010-05-30 20:00:00",
       "apparentTemperatureMin":43.31,
       "apparentTemperatureMinTime":"2010-05-30 06:00:00",
       "apparentTemperatureMax":58.99,
       "apparentTemperatureMaxTime":"2010-05-30 20:00:00"}]}]

目标是将这些行添加到上面的输入 excel 中。

icon                sunrisetime     sunsettime      etc.
NA                  NA              NA              etc.
partly-cloudy-day   18-03-10 07:22  18-03-10 19:25  etc.
partly-cloudy-day   30-05-10 05:38  30-05-10 22:44  etc.

【问题讨论】:

  • 我假设 JSON 是有效的,因为消息没有完整发布。也许它有助于部署一个支持 SQL 的服务器,它支持 JSON 以更容易地将 JSON 消息转换为记录/列。但这可能是一个很长的解决方法,因为我不知道您是否需要保存结果..
  • 在 MySQL 8 中或多或少是这样的,请参阅 demo 它应该可以为您节省一些在 R i geuss 中的编程
  • @raymond 啊,我以前从来没有用过,但从你的演示来看,它看起来很方便!如果它不能通过 R 运行,肯定会试一试(由于论文时间限制,所以现在使用新程序看起来更耗时),但是谢谢!
  • 祝你好运,因为我在 R 中无法真正帮助你,因为在我很可能弄清楚如何连接到 MySQL 的工作代码并查询它之前我从未使用过它,如果我会研究手册,另一方面,MySQL 是一个不同的故事,是的,使用 MySQL 使它非常方便和容易.. “在一点论文时间限制中,所以现在使用新程序看起来更耗时” 由于您在编程时经常存储/处理和操作数据,因此您将来学习 SQL 会很有意义,我猜

标签: r json dataframe


【解决方案1】:

处理返回 NULL 的响应时出现问题。为了简化问题,删除这些非响应然后解析剩余的 JSON 响应更容易。如果需要,可以返回并为未响应的内容添加空行。

library(jsonlite)
library(dplyr)

#test<- result from converting the JSON response.  
#vector of reviewid, used to make the initial request to the API
reviewid<-c(98338143, 58929813, 65945346)

#find only the responses that are not Null or blank
valid<-which(sapply(1:nrow(test),  function(j) {length(test[[1]][[j]])}) >0)
NullResponses<-which(sapply(1:nrow(test),  function(j) {length(test[[1]][[j]])}) == 0)

#create a list of data frames with the data from row of the response
dflist<-lapply( valid, function(j) {
  temp<-t(as.matrix(unlist(test[j,])))
  df<-data.frame(reviewid=reviewid[j], temp, stringsAsFactors = FALSE)
  df
  })
#bind the rows together.
answer<-bind_rows(dflist)

【讨论】:

  • 非常感谢您的回复,您是我的英雄!看起来这应该可以完成这项工作。我有一个问题,当我运行 valid 和 NullResponses 时,我不断收到错误消息:'Error in 1:nrow(test) : argument of length 0' 我不知道如何摆脱它。你有解决方案吗?
  • @EvelienvanderWaal,您可能没有任何 Null 空白响应,因此出现错误。你可以试试这个:which(!(1:nrow(test) %in% valid))
  • 啊,我试过了,但不幸的是,这给出了同样的错误。
  • @EvelienvanderWaal,我只能猜测您的所有内容都是完整的,并且没有空集,因此 which 函数返回一个空集。这行真的不需要,所以我会把它注释掉或移到代码块的末尾。
  • 它也会为有效响应提供此错误,因此可能是因为其他原因,但感谢您的宝贵时间!我会问别人。
猜你喜欢
  • 2016-08-30
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
  • 2014-11-12
  • 2014-06-18
  • 2021-05-27
  • 2021-09-06
  • 1970-01-01
相关资源
最近更新 更多