Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas map one column to the combination of two columns

I am working with a DataFrame which looks like this

List    Numb    Name
1       1       one
1       2       two
2       3       three
4       4       four
3       5       five

and I am trying to compute the following output.

List    Numb    Name
one     1       one
one     2       two
two     3       three
four    4       four
three   5       five

In my current approach I'm trying to iterate through the columns, then replace values with the contents of a third column.

For example, if List[0][1] is equal to Numb[1][1] replace column List[0][1] with 'one'.

How could I make an iteration like this work, or alternatively solve the problem without explicitly iterating at all?

like image 815
tejosa Avatar asked Dec 18 '18 21:12

tejosa


1 Answers

Use map

df['List'] = df['List'].map(df.set_index('Numb')['Name'])


    List    Numb    Name
0   one     1   one
1   one     2   two
2   two     3   three
3   four    4   four
4   three   5   five
like image 158
Vaishali Avatar answered Oct 07 '22 12:10

Vaishali