I try to figure out how to align the rows and columns of many DataFrames stored in two separate dictionaries.
To illustrate it, I created four DataFrames df1, df2, df3, and df4 which I stored in dictionaries:
df1:
Dates ID1 ID2 ID3 ID4 ID6
0 2021-01-01 0.0 0.0 0.1 0.0 0.0
1 2021-01-03 0.0 0.0 0.0 0.0 0.0
2 2021-01-04 0.1 0.1 0.0 0.1 0.1
3 2021-01-05 0.2 0.2 0.2 0.2 0.2
4 2021-01-06 0.1 0.1 0.4 0.1 0.1
df2:
Dates ID1 ID2 ID3 ID4 ID6
0 2021-01-01 0.00 0.0 0.1 0.0 0.0
1 2021-01-03 0.20 0.3 0.0 0.0 0.0
2 2021-01-04 0.15 -0.1 0.4 -0.1 -0.1
3 2021-01-05 0.20 -0.2 -0.2 0.3 0.2
4 2021-01-06 0.10 0.1 0.2 -0.1 0.1
df3:
Dates ID2 ID4 ID5 ID6
0 2021-01-01 3 1.0 4 2.0
1 2021-01-02 6 2.4 3 1.5
2 2021-01-03 3 -2.0 3 2.0
3 2021-01-04 -1 1.0 -3 -2.0
4 2021-01-05 3 4.0 4 3.0
df4:
Dates ID2 ID4 ID5 ID6
0 2021-01-02 6 2.4 3 1.5
1 2021-01-03 3 -2.0 3 2.0
2 2021-01-04 -1 1.0 -3 -2.0
3 2021-01-07 3 4.0 4 3.0
For reproducibility:
import pandas as pd
df1 = pd.DataFrame({
'Dates':['2021-01-01', '2021-01-03', '2021-01-04', '2021-01-05', '2021-01-06'],
'ID1':[0,0,0.1,0.2,0.1],
'ID2':[0,0,0.1,0.2,0.1],
'ID3':[0.1,0,0,0.2,0.4],
'ID4':[0,0,0.1,0.2,0.1],
'ID6':[0,0,0.1,0.2,0.1]})
df2 = pd.DataFrame({
'Dates':['2021-01-01', '2021-01-03', '2021-01-04', '2021-01-05', '2021-01-06'],
'ID1':[0,0.2,0.15,0.2,0.1],
'ID2':[0,0.3,-0.1,-0.2,0.1],
'ID3':[0.1,0,0.4,-0.2,0.2],
'ID4':[0,0,-0.1,0.3,-0.1],
'ID6':[0,0,-0.1,0.2,0.1]})
df3 = pd.DataFrame({
'Dates':['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05'],
'ID2':[3,6,3,-1,3],
'ID4':[1,2.4,-2,1,4],
'ID5':[4,3,3,-3,4],
'ID6':[2,1.5,2,-2,3]})
df4 = pd.DataFrame({
'Dates':['2021-01-02', '2021-01-03', '2021-01-04', '2021-01-07'],
'ID2':[6,3,-1,3],
'ID4':[2.4,-2,1,4],
'ID5':[3,3,-3,4],
'ID6':[1.5,2,-2,3]})
dic1 = dict()
dic1['df1'] = df1
dic1['df2'] = df2
dic2 = dict()
dic2['df3'] = df3
dic2['df4'] = df4
What I try to get is:
dic1['df1']:
Dates ID2 ID4 ID6
0 2021-01-03 0.0 0.0 0.0
1 2021-01-04 0.1 0.1 0.1
dic1['df2']:
Dates ID2 ID4 ID6
0 2021-01-03 0.3 0.0 0.0
1 2021-01-04 -0.1 -0.1 -0.1
dic2['df3']:
Dates ID2 ID4 ID6
0 2021-01-03 3 -2 2
1 2021-01-04 -1 1 -2
dic2['df4']:
Dates ID2 ID4 ID6
0 2021-01-03 3 -2 2
1 2021-01-04 -1 1 -2
I try to align the rows and columns of the two dictionaries so that I only include columns that are contained in all DataFrames of the different dictionaries. Is there an efficient command to deal with that in dictionaries?