Python PANDAS:合并"< =" SQL加入

时间:2018-03-07 01:46:08

标签: python pandas numpy merge

在pandas中,是否有一个等效的merge或merge_asof操作可以完成以下SQL的等效操作:

INNER JOIN number_table as n on n.N <= t.some_integer_field 

where n is a number/tally table dataframe with a single column of integers(1 to 1000)
and t is a table with some integer field you would like to "deaggregate"

任何提示都将非常感谢!

1 个答案:

答案 0 :(得分:1)

在SQL中,INNER JOIN不等同等同于CROSS JOINON可以替换为WHERE。从技术上讲,即使是平等!所以你需要:

INNER JOIN number_table as n ON n.N <= t.some_integer_field 

可以替换为:

CROSS JOIN number_table as n WHERE n.N <= t.some_integer_field

并且因为交叉连接是cartesian products,所以运行相同的process in pandas,在其中为相同值和merge的两个数据帧分配一个列,它返回两个行的所有可能组合设置,因为将对齐。

df_number['key'] = 1  # OR df_number.assign(key=1)
df_table['key'] = 1

# CROSS JOIN WITH CONDITIONAL FILTER
pd.merge(df_table, df_number, on='key').query('N < some_integer_field')

现在CJ的表现是另一个问题!