I have two dataframes like df1, df2 below. I would like to:
- filter df1: that is, remove rows and columns, so that it has the same index elements and columns as df2. The elements within the table for the columns and rows kept should be not be modified.
- In addition, I would like to organize the rows and columns of this 'filtered' dataframe so that it has rows and columns in the same order as df2.
Dataframe df1
is:
index | x_3 | x_1 | x_2 |
---|---|---|---|
10 | 110 | 126 | 112 |
11 | 131 | 140 | 143 |
12 | 130 | 128 | 116 |
13 | 118 | 150 | 125 |
14 | 102 | 117 | 110 |
15 | 103 | 105 | 148 |
16 | 116 | 114 | 114 |
17 | 120 | 132 | 110 |
..and a second dataframe (df2
) like:
index | x_1 | x_2 | x_3 |
---|---|---|---|
10 | 1 | 1 | 5 |
11 | 4 | 1 | 2 |
14 | 2 | 2 | 4 |
15 | 1 | 2 | 1 |
16 | 2 | 4 | 1 |
The final result would be df3, that is:
index | x_1 | x_2 | x_3 |
---|---|---|---|
10 | 126 | 112 | 110 |
11 | 140 | 143 | 131 |
14 | 117 | 110 | 102 |
15 | 105 | 148 | 103 |
16 | 114 | 114 | 116 |
Any insights?