如何解决ValueError:预期为1D数组,得到的数组形状为(y, x)

0 投票
0 回答
28 浏览
提问于 2025-04-12 09:03

我创建了一个类似于vlookup的函数

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

我正在尝试在两个大小不同的数据表上调用这个函数,它们有相同的唯一标识符。

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

输出:

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

y = 行,x = 列

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

我对这个函数本身已经成功了,这是我第一次尝试在形状不同的数据表上使用它。有没有人有经验可以绕过这个错误?

0 个回答

暂无回答

撰写回答