Skip to content
Advertisement

Concat multiple dataframe and manage those that doesn’t exist

I try to concat some dataframe – 30 dataframe of 24h data – that been created automatically with some csv, but sometimes csv doesn’t exist, so the dataframe wasn’t created (df1, fd2, df4,df8,df9,…).

And so I want to create weekly dataframe with 7 concatenated df, but the function is in error when some df is missing. But I want the weekly df created with a empty period if some is missing.

JavaScript

When I execute line by line for testing :

JavaScript

df14 is missing, so dfweek2 is not created.

######################

EDIT : here is all my code for more comprehensive, maybe (probably) I didn’t choose the best way… Ignore all the interpolating things, that for later (maybe).

######################

JavaScript

Advertisement

Answer

I find something that’s working, even if I think that’s not the best way, here is my code, that a little long :

synthesis : The syntax globals()[f”df{x}”] is pretty useful to create new datarame automatically in loop, “df” is the name of the dataframe and “x” is its number (see below), but you have to create a dictionary first with df = {}.

Note : unlike my first post, in this code I replaced the ‘arg’ def part with a constant value ‘date’, during my development.

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