Home > Mobile >  Join column in dataframe to another dataframe - Pandas
Join column in dataframe to another dataframe - Pandas

Time:10-06

I have 2 dataframes. One has a bunch of columns including f_uuid. The other dataframe has 2 columns, f_uuid and i_uuid.

the first dataframe may contain some f_uuids that the second dataframe doesn't and vice versa.

I want the first dataframe to have a new column i_uuid (from the second dataframe) populated with the appropriate values for the matching f_uuid in that first dataframe.

How would I achieve this?

CodePudding user response:

df1 = pd.merge(df1,
               df2,
               on='f_uuid')

If you want to keep all f_uuid from df1 (e.g. those not available in df2), you may run

df1 = pd.merge(df1,
               df2,
               on='f_uuid',
               how='left')

CodePudding user response:

I think what your looking for is a merge : https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html?highlight=merge#pandas.DataFrame.merge

In your case, that would look like :

bunch_of_col_df.merge(other_df, on="f_uuid")
  • Related