Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

How to create a new column in a pandas Dataframe based on a value in other Dataframe [duplicate]

$
0
0

rPobably a simple question but I can't find the correct instruction to do it easily.

I have 2 dataframes with the following structure.

Dataframe 1Name1 Name2  Col1   Col2     Col3    Col4aaa   bbb    1         3       5      7ccc   ddd    9         8       6      4eee   fff    2        10      11     12ggg   hhh    13       15      17     19aaa   jjj    25       35      23     41...Dataframe 2Name  ColA  ColBaaa    100   200ggg    300   400eee    500   600ccc    700   800...

I need to add 2 columns to Dataframe 1 that contain the values in ColA and ColB in Dataframe 2 where Name1 in dataframe1 is equal to name in dataframe2.

To obtain the following:

Dataframe 1Name1 Name2  Col1   Col2     Col3    Col4  ColA   ColBaaa   bbb    1         3       5      7     100    200ccc   ddd    9         8       6      4     700    800eee   fff    2        10      11     12     500    600ggg   hhh    13       15      17     19     300    400aaa   bbb    25       35      23     41     100    200...

Note that Dataframe1 can have several times aaa, bbb, ccc but dataframe2 have onlu one time this value


Viewing all articles
Browse latest Browse all 23131

Trending Articles