外部联接表 - 保留描述

发布于 2025-01-17 16:02:57 字数 692 浏览 2 评论 0原文

new = pd.DataFrame({'table': \['a','b', 'c', 'd'\], 'desc': \['','','',''\], 'total':\[22,22,22,22\]})
old = pd.DataFrame({'table': \['a','b', 'e'\], 'desc': \['foo','foo','foo'\], 'total':\[11,11,11\]})

all = pd.merge(new, old, how='outer', on=\['table', 'total'\])

输出:

table desc_x  total desc_y
0     a            22    NaN
1     b            22    NaN
2     c            22    NaN
3     d            22    NaN
4     a    NaN     11    foo

所需的输出:

table desc  total
0     a   foo     22
1     b   foo     22
2     c           22
3     d           22
4     a   foo     11

我尝试进行外部连接,但它删除了A和B的描述。

new = pd.DataFrame({'table': \['a','b', 'c', 'd'\], 'desc': \['','','',''\], 'total':\[22,22,22,22\]})
old = pd.DataFrame({'table': \['a','b', 'e'\], 'desc': \['foo','foo','foo'\], 'total':\[11,11,11\]})

all = pd.merge(new, old, how='outer', on=\['table', 'total'\])

Output:

table desc_x  total desc_y
0     a            22    NaN
1     b            22    NaN
2     c            22    NaN
3     d            22    NaN
4     a    NaN     11    foo

Desired Output:

table desc  total
0     a   foo     22
1     b   foo     22
2     c           22
3     d           22
4     a   foo     11

I attempted to outer join, but it removed the descriptions of a and b.`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2025-01-24 16:02:57
  • 鉴于您想要实现的目标是在 tabletotal 上进行外部联接,这是没有意义的。更改为 table 上的外连接
  • 然后可以修改表以使用所需输出和清理列中隐含的首选项
new = pd.DataFrame({'table': ['a','b', 'c', 'd'], 'desc': ['','','',''], 'total':[22,22,22,22]})
old = pd.DataFrame({'table': ['a','b', 'e'], 'desc': ['foo','foo','foo'], 'total':[11,11,11]})

all = pd.merge(new, old, how='outer', on=['table'])

# select prefered columns
all["desc"] = all["desc_x"].replace('', np.nan).fillna(all["desc_y"]).fillna("")
all["total"] = all["total_x"].fillna(all["total_y"])

# clean up columns
all = all.drop(columns=[c for c in all.columns if c[-2:] in ["_x", "_y"]])

all
tabledescTotal
0afoo22
1bfoo22
2c22
3d22
4e11
  • it makes no sense given what you are trying to achieve is to outer join on table and total. Changed to outer join on table
  • table can then be modified to use preference that is implied in your desired output and cleanup columns
new = pd.DataFrame({'table': ['a','b', 'c', 'd'], 'desc': ['','','',''], 'total':[22,22,22,22]})
old = pd.DataFrame({'table': ['a','b', 'e'], 'desc': ['foo','foo','foo'], 'total':[11,11,11]})

all = pd.merge(new, old, how='outer', on=['table'])

# select prefered columns
all["desc"] = all["desc_x"].replace('', np.nan).fillna(all["desc_y"]).fillna("")
all["total"] = all["total_x"].fillna(all["total_y"])

# clean up columns
all = all.drop(columns=[c for c in all.columns if c[-2:] in ["_x", "_y"]])

all
tabledesctotal
0afoo22
1bfoo22
2c22
3d22
4efoo11
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文