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

Time:11-21

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

_ | 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    | 

CodePudding user response:

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

  • Related