【问题标题】:EDIT: Pyspark: drop rows based on another dataframe with conditions on many columns编辑:Pyspark:基于另一个数据框删除行,条件多列
【发布时间】:2020-09-25 08:59:08
【问题描述】:

我有两个数据框 df1 和 df2。我想根据 df2 列值过滤或删除 df1 上的行

df2,我必须检查客户名称、产品、年份、数量和金额,然后如果 df1 的所有值都相同,我必须放弃。 (即)如果 customername = Gopi、product = ball、year = 2020、qty = 0 和 amount

请告诉我如何实现,谢谢。

df1:

customername     product      year    qty     amount
Gopi             ball         2020    0       -100
Hana             ball         2020    2        200
Gopi             toy          2019    10       1000
Mesh             toy          2020    2        200
Tiby             ball         2017    1        100
Mona             pen          2020    1         70
Hana             ball         2019    2        200
Mona             ball         2020    1         70
Mona             toy          2020    1         70
Tiby             toy          2020    1        100

df2:

customername     product      year    qty     amount
Gopi             ball         2020    0       -100
Mona             pen          2020    1         70

预期输出:

customername     product      year    qty     amount
Hana             ball         2020    2        200
Gopi             toy          2019    10       1000
Mesh             toy          2020    2        200
Tiby             ball         2017    1        100
Hana             ball         2019    2        200
Mona             ball         2020    1         70
Mona             toy          2020    1         70
Tiby             toy          2020    1        100

【问题讨论】:

    标签: python-3.x dataframe pyspark


    【解决方案1】:

    我设法过滤如下。

    final_df = df1.join(df2, ((df1.customername == df2.customername) & (df1.product == df2.product) & (df1.year == df2.year) & (df1.qty == df2.qty) & (df1.amount <= 0), how='left_anti'))
    

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      • 2022-11-15
      • 1970-01-01
      • 2021-12-13
      • 2018-12-24
      相关资源
      最近更新 更多