我在 df1 中有 3 个数据框
| srno | col1 | col2 | col3 | col4 |
|---|---|---|---|---|
| 1 | a1 | a2 | a3 | |
| 2 | b1 | c2 | c3 | |
| 3 | d1 | b2 | ||
| 4 | e1 | e2 | e3 |
df2
| srno | col1 | col2 | col3 | col4 |
|---|---|---|---|---|
| 1 | a1 | g1 | ||
| 2 | b2 | g2 | ||
| 3 | c2 | c3 | g3 |
df3
| 优先 | col_combination |
|---|---|
| 1 | col1 |
| 2 | col2,col3 |
我正在寻找以下输出 df1
| srno | col1 | col2 | col3 | col4 |
|---|---|---|---|---|
| 1 | a1 | a2 | a3 | g1 |
| 2 | b1 | c2 | c3 | g3 |
| 3 | d1 | b2 | g2 | |
| 4 | e1 | e2 | e3 |
我尝试了多种方法但无法实现这一点,我是 Python 编码的新手,有什么方法可以实现吗?下面的代码我试过它确实匹配并返回找到/未找到但还不能分配 df1[col4] = df2[col4] 匹配行。
for i in df3.index:
if "," in df3.loc[i,"col_combination"]:
print("multi column values to handle later")
else:
df1['col4'] = np.where(df1[df3.loc[i, "col_combination"]].isin(df2[df3.loc[i, "col_combination"]]),'found','not found')