How to resolve ValueError: Expected a 1D array, got an array with shape (y, x)

31 Views Asked by At

I created a vlookup-esque function

def lookup(df1, df2, index, value, is_sorted=False):
    df_merge = df2.merge(df1[[index, value]], how='left', on=index)
    return df_merge

I am attempting to call the function on two DataFrames of varying size, they share the same unique identifiers.

df2 = lookup(d1, df2, 'col_unique', 'col_value', is_sorted=True)

Output:

ValueError: Expected a 1D array, got an array with shape (y, x)

y = rows, x = col

df1 = {'Order_No': [123, 456, 789], 'Shipping_Stat': ['Delivered', 'In-Progress', 'Delivered']}
df2 = {'Order_No': [123, 456, 789, 999], 'Name': ['John B', 'Alex A', 'Frank L', 'Louis']

Expected Output:
Order_No   Name   Shipping_Stat
123       John B  Delivered
456       Alex A  In-Progress
789       Frank L Delivered

I have had success with the function itself, this is my first attempt using dataframes of varying shape. Has anyone had experience bypassing this error?

0

There are 0 best solutions below