NsN
NsN

Reputation: 109

Python - A dictionary of dataframes: merge all the dataframes to one big dataframe

I have a dictionary of dataframes. It consists of around 50 dfs, but for the simplicity of demonstration, let's say I only have 2.

This is the dictionary: it's a lot of weather parameters for a specific location, for several days.

dict_df = { "Location_1" : [ temp_max  temp_min  precip_mm  \
date                                                         
2012-05-16      31.370001      15.050000            0.0000   
2012-05-17      30.559999      16.780001            0.0000   
2012-05-18      32.529999      17.040001            0.0000   
2012-05-19      32.860001      19.190001            0.0000   
2012-05-20      33.340000      18.580000            0.0000   
2012-05-21      27.430000      17.450001           18.5245   
2012-05-22      26.730000      13.800000            0.0000   
2012-05-23      29.340000      13.300000            0.0000   
2012-05-24      32.779999      19.500000            0.0000   
2012-05-25      32.919998      22.830000            0.0000   

            solar_energy_w_h_per_m2  rel_humidity_max_%  rel_humidity_min_%  
date                                                                          
2012-05-16              7677.530273           83.779999           24.580000   
2012-05-17              7488.292969           78.629997           25.270000   
2012-05-18              6644.316895           83.879997           26.900000   
2012-05-19              7523.830078           83.709999           33.230000   
2012-05-20              6840.391113           90.139999           33.930000   
2012-05-21              5472.107910           93.139999           43.490002   
2012-05-22              8293.391602           87.540001           28.680000   
2012-05-23              8351.654297           91.379997           25.240000   
2012-05-24              8176.128418           69.089996           35.290001   
2012-05-25              6369.352539           76.449997           40.139999 ],
"Location_2" : [temp_max_cels  temp_min_cels  precip_amount_mm  \
date                                                         
2012-05-16      31.370001      15.050000            0.0000   
2012-05-17      30.559999      16.780001            0.0000   
2012-05-18      32.529999      17.040001            0.0000   
2012-05-19      32.860001      19.190001            0.0000   
2012-05-20      33.340000      18.580000            0.0000   
2012-05-21      27.430000      17.450001           18.5245   
2012-05-22      26.730000      13.800000            0.0000   
2012-05-23      29.340000      13.300000            0.0000   
2012-05-24      32.779999      19.500000            0.0000   
2012-05-25      32.919998      22.830000            0.0000   

            solar_energy_w_h_per_m2  rel_humidity_max_%  rel_humidity_min_%  \
date                                                                          
2012-05-16              7677.530273           83.779999           24.580000   
2012-05-17              7488.292969           78.629997           25.270000   
2012-05-18              6644.316895           83.879997           26.900000   
2012-05-19              7523.830078           83.709999           33.230000   
2012-05-20              6840.391113           90.139999           33.930000   
2012-05-21              5472.107910           93.139999           43.490002   
2012-05-22              8293.391602           87.540001           28.680000   
2012-05-23              8351.654297           91.379997           25.240000   
2012-05-24              8176.128418           69.089996           35.290001   
2012-05-25              6369.352539           76.449997           40.139999]}   

And it goes like that for 50 locations or so.

I want to be able to merge all the dataframes from the dictionary that may not have the exact dates but all have the same type and number of columns to a dataframe like this:

enter image description here

I hope it's clear. I really appreciate any help you can provide.

Upvotes: 1

Views: 102

Answers (1)

BENY
BENY

Reputation: 323316

You can just do

df = pd.concat(dict_df).reset_index(level=1,drop=True)

Upvotes: 1

Related Questions