LearnerBegineer
LearnerBegineer

Reputation: 23

Pandas Merge ValueError: operands could not be broadcast together with shapes (323,) (324,) ()

Hi I am trying to merge two dataframes inside np.where but getting error. How to achive df.merge() , What am i doing wrong ?

Code:

df3['old_result'] = np.where((df3['present_in_old'] == 'yes'), df3.merge(df1,left_on=(df3['id']), right_on = (df1['id']), how = 'outer')['name'],None)

Upvotes: 0

Views: 148

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31166

  • generated some sample data with df1 and df3 different sizes.
  • your core issue - need a left join not an outer join
  • you can also just use column names as left_on / right_on parameters
  • due to fact I have duplicate column names in sample data I also uses suffixes parameter
df1 = pd.DataFrame(
    {"id": range(300), "name": np.random.choice(list("abcdefghijkjlmnopqrstuvwxyz"), 300)}
)
df3 = pd.DataFrame(
    {
        "id": range(35),
        "name": np.random.choice(list("abcdefghijkjlmnopqrstuvwxyz"), 35),
        "present_in_old": np.random.choice(["yes", "no", "maybe"], 35),
    }
)


df3["old_result"] = np.where(
    (df3["present_in_old"] == "yes"),
    df3.merge(df1, left_on="id", right_on="id", suffixes=("_left", ""), how="left")["name"],
    None,
)

Upvotes: 1

Related Questions