相当于SQL CROSS JOIN的 pandas (笛卡尔积) [英] Pandas Equivalent of SQL CROSS JOIN (Cartesian Product)
本文介绍了相当于SQL CROSS JOIN的 pandas (笛卡尔积)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有两个桌子:
表1:
col1 col2
0 1
2 3
表2:
col3 col4
5 6
7 8
在SQL中,如果我做了以下声明:
In SQL, if I made the following statement:
Select *
From Table1, Table2;
我希望从两个表中获得一个包含所有组合的表:
I would expect to get back a table with all combinations from both tables:
col1 col2 col3 col4
0 1 5 6
0 1 7 8
2 3 5 6
2 3 7 8
有没有办法对熊猫中的两个数据框做同样的事情?
Is there a way to do the same thing with two dataframes in pandas?
推荐答案
标准习惯用法是在虚拟列上使用merge
.
A standard idiom is using the merge
on a dummy column.
df1.assign(foo=1).merge(df2.assign(foo=1)).drop('foo', 1)
col1 col2 col3 col4
0 0 1 5 6
1 0 1 7 8
2 2 3 5 6
3 2 3 7 8
这篇关于相当于SQL CROSS JOIN的 pandas (笛卡尔积)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文