Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding a new column in one DataFrame where values are based from a second DataFrame

I have two DataFrames, df_a is the DataFrame we want to manipulate. I want to add a new column but the values are found in a second DataFrame with a similar column name.

Let me expound.

df_a contains

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

_ | Code | Speed | Velocity |
0 |  DA  |   23  |    22    |
1 |  ES  |   23  |    22    |
2 |  DA  |   23  |    22    |
3 |  GA  |   23  |    22    |
4 |  NU  |   23  |    22    |

df_b contains

_ | Code |     Name    |
0 |  DA  |   DinoAero  |
1 |  ES  |    Espeed   |
2 |  GA  |    GeoArk   |
3 |  NU  |  NewUnicorn |

I want to merge or concatenate these two DataFrames that the result should look like this:

_ | Code |     Name     | Speed | Velocity |
0 |  DA  |   DinoAero   |   23  |    22    |
1 |  ES  |    Espeed    |   23  |    22    |
2 |  DA  |   DinoAero   |   23  |    22    |
3 |  GA  |    GeoArk    |   23  |    22    |
4 |  NU  |  NewUnicorn  |   23  |    22    | 

I’m quite new to both Python and Pandas, I am having a hard time also how to search the proper question for this so I’ve decided to ask in stackoverflow and risk my downvote just for me to learn. Appreciate the patience for everyone willing to share wisdom.

>Solution :

You just want to pd.merge() (which is similar to a SQL join).

In your case:

new_df = pd.merge(df_a,df_b,how='left',on='Code')
new_df = new_df[['Code','Name','Speed','Velocity']] # if you want to re-arrange the columns in your order

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading