Skip to content
Advertisement

Iterating through JSON and appending into dataframe

I’m getting weather forecasting data from weatherstack API.

JavaScript

The output looks like this:

JavaScript

How do I iterate through this output and create a new data frame to look like this: (And get only the date time hour, and rain values)

Date precip at time 0 precip at time 300
2021-05-11 0.1 0
2021-05-12 0.1 0

Also, I want to convert the date column to Month Day ,Year. ie. 2021-05-11 -> May 05, 2021. I tried using

JavaScript

then

JavaScript

But I get this error:

JavaScript

Advertisement

Answer

Create dataframe

This will create a dataframe with the required data and column names.

JavaScript

Create dataframe and format headers

If you wanted the times in HH:MM format make these changes/additions to the code.

JavaScript

Change Date column format

To change the format of the Date column we first need to convert it to a datetime, which we can do using pd.to_datetime.

We can then chain strftime to get the required format, May 05, 2021.

For a full list of the directives you can use with strftime see here.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement