简体   繁体   中英

Perform a lookup for each column individually Python Pandas

For a dataframe I would like to perform a lookup for every column and place the results in the neighbouring column. id_df contains the IDs and looks as following:

            Col1           Col2  ...               Col160                 Col161
0          4328.0        4561.0  ...                NaN                   5828.0
1          3587.0        4328.0  ...                NaN                  20572.0
2          4454.0        1702.0  ...                NaN                    683.0

lookup_df also contains the ID and a value that I'm interested in. lookup_df looks as following:

    ID       Value
0   3587    3.0650
1   4454    2.9000
2   5       2.8450
3   8       2.8750
4   11      3.1000
5   13      3.1600
6   16      2.4450
7   18      3.0700
8   20      2.7950
9   23      3.0500
10   25      3.2250

I would like to get the following Dataframe df3:

           Col1ID        Col1 Value   ...               Col161 ID             Col161 Value
0          4328.0        2.4450       ...                5828.0                   3.1600
1          3587.0        3.2250       ...                20572.0                  3.0650
2          4454.0        3.0500       ...                 683.0                   3.1600

Because I'm an excel user I thought of using the function 'merge', but I don't see how this can be done with multiple columns.

Thank you!

Use map :

m = lookup_df.set_index('ID')['Value']

result = pd.DataFrame()
for col in id_df.columns:
    result[col + '_ID'] = df[col]
    result[col + '_Value'] = df[col].map(m)

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