简体   繁体   中英

How to merge for two different rows in PANDAS?

I want to merge two dataframes. The left dataframe has two identifiers, id1 and id2. The right dataframe has the string version of those identifiers. What I want to do is get both ids and the string version of both ids in the same row. Example:

left:     right:
id1 id2   id string
0   1     0  "a"
3   4     1  "b"
10  0     3  "c"
1   4     4  "d"
          10 "e"

Output of merging:

id1 id2 string1 string2
0   1   "a"     "b"
3   4   "c"     "d"
10  0   "e"     "a"
1   4   "b"     "d"

How would I do this?

Creating a mapper from the right DataFrame is probably best here then using Series.map on each column as it scales up very easily:

mapper = right.set_index('id')['string']
merged = left.copy()
for i, col in enumerate(merged.columns, 1):
    merged[f'{mapper.name}{i}'] = merged[col].map(mapper)

Alternatively with chained merge calls:

merged = (
    left.merge(right.rename(columns={'id': 'id1'}), on='id1', how='left')
        .merge(right.rename(columns={'id': 'id2'}), on='id2', how='left',
               suffixes=('1', '2'))
)

Both produce merged :

   id1  id2 string1 string2
0    0    1       a       b
1    3    4       c       d
2   10    0       e       a
3    1    4       b       d

DataFrames:

import pandas as pd

left = pd.DataFrame({
    'id1': {0: 0, 1: 3, 2: 10, 3: 1},
    'id2': {0: 1, 1: 4, 2: 0, 3: 4}
})

right = pd.DataFrame({
    'id': {0: 0, 1: 1, 2: 3, 3: 4, 4: 10},
    'string': {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}
})

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