简体   繁体   中英

Matching IDs with names on a pandas DataFrame

I have a DataFrame with 3 columns: ID, BossID and Name. Each row has a unique ID and has a corresponding name. BossID is the ID of the boss of the person in that row. Suppose I have the following DataFrame:

df = pd.DataFrame({'id':[1,2,3,4,5], 'bossId':[np.nan, 1, 2, 2, 3], 
                   'name':['Anne Boe','Ben Coe','Cate Doe','Dan Ewe','Erin Aoi']})

So here, Anne is Ben's boss and Ben Coe is Cate and Dan's boss, etc.

Now, I want to have another column that has the boss's name for each person.

The desired output is:

    id  boss    name    boss_name
0   1   NaN     Anne    NaN
1   2   1.0     Ben     Anne
2   3   2.0     Cate    Ben
3   4   2.0     Dan     Ben
4   5   3.0     Erin    Cate

I can get my output using an ugly double for-loop. Is there a cleaner way to obtain the desired output?

This should work:

bossmap = df.set_index('id')['name'].squeeze()
df['boss_name'] = df['bossId'].map(bossmap)
  • Create a separate dataframe for 'name' and 'id' .
    • Renamed 'name' and set 'id' as the index
  • .merge df with the new dataframe
import pandas as pd

# test dataframe
df = pd.DataFrame({'id':[1,2,3,4,5], 'bossId':[np.nan, 1, 2, 2, 3], 'name':['Anne Boe','Ben Coe','Cate Doe','Dan Ewe','Erin Aoi']})

# separate dataframe with id and name
names = df[['id', 'name']].dropna().set_index('id').rename(columns={'name': 'boss_name'})

# merge the two
df = df.merge(names, left_on='bossId', right_index=True, how='left')

# df
   id  bossId      name boss_name
0   1     NaN  Anne Boe       NaN
1   2     1.0   Ben Coe  Anne Boe
2   3     2.0  Cate Doe   Ben Coe
3   4     2.0   Dan Ewe   Ben Coe
4   5     3.0  Erin Aoi  Cate Doe

You can set id as index, then use pd.Series.reindex

df = df.set_index('id')
df['boss_name'] = df['name'].reindex(df['bossId']).to_numpy() # or .to_list()

  id  bossId      name boss_name
0   1     NaN  Anne Boe       NaN
1   2     1.0   Ben Coe  Anne Boe
2   3     2.0  Cate Doe   Ben Coe
3   4     2.0   Dan Ewe   Ben Coe
4   5     3.0  Erin Aoi  Cate Doe

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