【发布时间】: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 列。缺少第一个条目,因为当天没有观察并且未显示)
[{},
{"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 会很有意义,我猜