Skip to content Skip to sidebar Skip to footer

Merging All Columns Of Pandas Dataframes

I have many pandas DataFrames for stocks. They all have the form: df_asset = pd.DataFrame(data=np.random.random((5,4)), index=[0, 1, 2, 3, 4], columns=['Open', 'High', 'Low', 'Clos

Solution 1:

You can concatenate a dict of DataFrames, dfs, into a single DataFrame using

df = pd.concat(dfs)

df will use the dict keys as a MultiIndex level.


For example,

In [85]: dfs = {'AAPL': df_asset, 'CSCO': df_asset}

In [86]: df = pd.concat(dfs); df
Out[86]: 
            Open      High       Low     Close
AAPL 0  0.100276  0.769425  0.060993  0.831183
     1  0.251792  0.336571  0.976984  0.237506
     2  0.611914  0.029576  0.329525  0.203794
     3  0.527770  0.723468  0.887708  0.231006
     4  0.965805  0.508156  0.260214  0.063260
CSCO 0  0.100276  0.769425  0.060993  0.831183
     1  0.251792  0.336571  0.976984  0.237506
     2  0.611914  0.029576  0.329525  0.203794
     3  0.527770  0.723468  0.887708  0.231006
     4  0.965805  0.508156  0.260214  0.063260

To get the index levels in the order that you posted in your question, use swaplevel followed by sort_index:

In [112]: df.swaplevel().sort_index()
Out[112]: 
            Open      High       Low     Close
0 AAPL  0.1002760.7694250.0609930.831183
  CSCO  0.1002760.7694250.0609930.8311831 AAPL  0.2517920.3365710.9769840.237506
  CSCO  0.2517920.3365710.9769840.2375062 AAPL  0.6119140.0295760.3295250.203794
  CSCO  0.6119140.0295760.3295250.2037943 AAPL  0.5277700.7234680.8877080.231006
  CSCO  0.5277700.7234680.8877080.2310064 AAPL  0.9658050.5081560.2602140.063260
  CSCO  0.9658050.5081560.2602140.063260

Post a Comment for "Merging All Columns Of Pandas Dataframes"