Ed Jefferies
Ed Jefferies

Reputation: 207

create pandas column with new values based on values in other columns

dataframe a:

    id    value
-------------------------------
0   231   9243
1   392   81139
2   493   896546
3   879   31

dataframe b:

   id   description  colour
-------------------------------
0  231  football     brown
1  392  bat          white
2  556  chicken      yellow 
3  493  tennis ball  yellow
4  119  pig          pink
5  879  cricket ball red

My question is how can I create a new column in dataframe a that inputs the description from dataframe b based on matching to the id column values in dataframe a? So I end up with:

    id    description    value    
-------------------------------
0   231   football        9243
1   392   bat             81139
2   493   tennis ball     896546
3   879   cricket ball    31

Upvotes: 2

Views: 60

Answers (2)

SeaBean
SeaBean

Reputation: 23237

You can create a dict for mapping id and description from dataframe b and then use .map() on dataframe a on id columns, as follows:

b_dict = dict(zip(b['id'], b['description']))
a['description'] = a['id'].map(b_dict).fillna('')

Result:

print(a)

    id   value   description
0  231    9243      football
1  392   81139           bat
2  493  896546   tennis ball
3  879      31  cricket ball

Upvotes: 1

Nk03
Nk03

Reputation: 14949

via left merge

df1 = df1.merge(df2[['id','description']], on='id', how='left')

output

    id   value   description
0  231    9243      football
1  392   81139           bat
2  493  896546   tennis_ball
3  879      31  cricket_ball

Upvotes: 1

Related Questions