合并 pandas 2列中任一列上的表 [英] Merge table on either of the 2 columns in pandas

查看:65
本文介绍了合并 pandas 2列中任一列上的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究使用pandas合并表的python,但是我没有什么麻烦.这是问题所在.

I am working on python to merge a table using pandas, but I am having little trouble. Here's the problem.

我有2个table_A和table_B.我在table_A上有两列,分别是一个",两个".我在table_B上有两个列,分别说一个",三个". table_B中的一"列具有一些值,它们映射到table_A中的一"列,而某些值映射到table_A中的列二".如何在python中映射类似于查询的值:

I have 2 tables_A and table_B. I have two columns on table_A say "one", "two". I have two column on table_B say "one","three". column "one" in table_B has some values which maps to column "one" in table_A and some values which maps to column "two" in table_A. How can I map values in python similar to query:

Select * from Table_A a, Table_B b
where a.one = b.one or a.two=b.one 

谢谢!

推荐答案

考虑使用合并的串联,它将您的SQL查询转换为OR,通常类似于UNION:

Consider a concatenation with merge which would translate your SQL query as OR is often analogous to a UNION:

pd.concat([pd.merge(table_A, table_B, on='one'),
           pd.merge(table_A, table_B, left_on='two', right_on='one')])

这篇关于合并 pandas 2列中任一列上的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆