Luka Vlaskalic
Luka Vlaskalic

Reputation: 465

How to use pd.concat to merge multiple DataFrames together in a For Loop

I am using the Dark Sky API and the darkskylib library to create a yearly, hourly forecast for New York City.

nyc.hourly returns a DataBlock with all weather data, from which I can call the temperature for the next 24hrs.

Basically, my problem is that the variable holding does not seem to add the temperature from the two dates together, but just returns the last one. I think that I have got all of my indentation right, but maybe not.

import time
import pandas as pd

from darksky import forecast
NYC = 'API Key',40.7128,-74.0060

from datetime import date, timedelta, datetime

l = 2

for i in range(0,l):
    nyc = forecast(*NYC, time=date_list[i])

    nyc.refresh(units='si', extend='hourly')
    # change units to SI units

    n = len(nyc.hourly)

    temp = []
    unix_time = []
    year = []

    # create a list of hourly temperatures for the day in question
    for i in range(0,n):
        hourly_temp = nyc.hourly[i].temperature
        temp.append(hourly_temp)
    year.append(temp)
    holding = pd.DataFrame(temp)
final = pd.concat([holding], ignore_index=True)

note; I define date_list at the beginning of the code, it is a bit long, but this is what it returns, and its entries are strings.

>>> date_list
['2016-01-01T00:00:00', '2016-01-02T00:00:00']

Upvotes: 2

Views: 543

Answers (1)

jpp
jpp

Reputation: 164623

Try this set-up. You need to store all the holding dataframes and combine them at the end. Dictionaries are a convenient way to do this.

holding = {}
l = 2

for i in range(0, l):
    # perform calculations
    holding[i] = pd.DataFrame(temp)

final = pd.concat(list(holding.values()), ignore_index=True)

Upvotes: 2

Related Questions