【问题标题】:Cross join not working correctly in pandas交叉连接在熊猫中无法正常工作
【发布时间】:2022-08-09 15:53:57
【问题描述】:

我有 2 个熊猫表 table_a 和 table_b 两个表都包含相同的列 ID 和 no_of_employee

我需要在我尝试以下语法的两个表上交叉

table_a[\'key\'] = 1
table_b[\'key\'] = 1

df_detail = pd.merge(table_a, table_b, on =\'key\').drop(\"key\", 1)

但它没有正常工作,因为下面的输出包含四列:

[\'ID_x\',\'no_of_employee_x\',\'ID_y\',\'no_of_employee_y\']

但我只需要下面给出的最终表中的 2 列:

Idno_of_employee

什么是正确的语法?我试图找到但没有找到。

  • 你在使用 pyspark 还是 pandas?
  • @samkart Pandas 我正在使用
  • 看起来你不需要交叉连接,使用外连接

标签: pandas cross-join


【解决方案1】:

我想这可能是你想要的

import pandas as pd

d1 = [(1, 15), (7, 12)]
df1 = pd.DataFrame(data=d1, columns=['id', 'employee_no'])

d2 = [(1, 3), (4, 8)]
df2 = pd.DataFrame(data=d2, columns=['id', 'employee_no'])

df_cross = df1.merge(df2, how='outer', sort=True)

print(df_cross)

   id  employee_no
0   1            3
1   1           15
2   4            8
3   7           12

【讨论】:

  • 抱歉,这是熊猫表而不是 pyspark,你能提供熊猫的语法吗
  • 我更新了答案。如果那不完全是你所期望的。你可以分享一些例子。
猜你喜欢
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
相关资源
最近更新 更多