Home > Net >  Find "Guest" from df1 in df2 and create 3rd DataFrame with the combined info
Find "Guest" from df1 in df2 and create 3rd DataFrame with the combined info

Time:11-02

I have 2 DataFrames df1 and df2. The two df's are of different length. df1 is appointments and df2 is sales. I will eventually want to find sales % of consults (df1['Service']) that turned into a sale (this is end game not needed at the moment).

For now I want to find if "Guest" in df1 is also in df2 (meaning a service/consult turned into a sale) and if so I want a 3rd DataFrame created with the combined data of 'Guest' from df1 and df2.

df1[['Guest', 'Therapist', 'Service', 'StartTime', 'Status']]

df2[['Guest', 'ProductName', 'Quantity']]

CodePudding user response:

Try with where and isin:

df3 = df1["Guest"].where(df1["Guest"].isin(df2["Guest"].tolist())).dropna()

To get a DataFrame with all columns, you could use merge instead:

df3 = df1.merge(df2, on="Guest")
  • Related