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.100276  0.769425  0.060993  0.831183
  CSCO  0.100276  0.769425  0.060993  0.831183
1 AAPL  0.251792  0.336571  0.976984  0.237506
  CSCO  0.251792  0.336571  0.976984  0.237506
2 AAPL  0.611914  0.029576  0.329525  0.203794
  CSCO  0.611914  0.029576  0.329525  0.203794
3 AAPL  0.527770  0.723468  0.887708  0.231006
  CSCO  0.527770  0.723468  0.887708  0.231006
4 AAPL  0.965805  0.508156  0.260214  0.063260
  CSCO  0.965805  0.508156  0.260214  0.063260

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