简体   繁体   中英

Python/Pandas: filter and organize the rows and columns of a dataframe based on another dataframe

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?

You can use .reindex_like to conform the index and columns of df1 according to index and columns of df2 :

df3 = df1.reindex_like(df2)

>>> df3

       x_1  x_2  x_3
index               
10     126  112  110
11     140  143  131
14     117  110  102
15     105  148  103
16     114  114  116
df1.loc[df2.index, df2.columns]

Shubham's answer is quite pythonic. Using loc is also a simple way to go from first principles.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM