简体   繁体   中英

How to only merge one column into a Pandas DataFrame

I have a dataframe called 'ids':

id   name
a1   Jake
b2   Sully

that I want to merge into 'data':

identifier   value
        a1     111
        b2     222

to achieve this:

identifier   value    name
        a1     111    Jake
        b2     222    Sully

However when I call data = pd.merge(data, ids, left_on='identifier', right_on='id')

it adds 'id' again.

  identifier  value  id   name
0         a1    111  a1   jake
1         b2    222  b2  sully


ids = pd.DataFrame({'id': ['a1', 'b2'], 'name': ['jake', 'sully']})
data = pd.DataFrame({'identifier': ['a1', 'b2'], 'value': [111, 222]})
print(ids)
print(data)
data = pd.merge(data, ids, left_on='identifier', right_on='id')
print(data)

How can I merge without double adding 'id' so I don't have to manually drop it after?

We can rename it

data = pd.merge(data, ids.rename(columns={'id':'identifier'}), on='identifier')
data
  identifier  value   name
0         a1    111   jake
1         b2    222  sully

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