简体   繁体   English

如果数据匹配并插入新数据,则根据另一个数据框删除行

[英]Delete rows on the basis of another data frame if the data matched and insert new data

I have two files one is file1.csv and another one is file2.csv I have put file1 data in one dataframe and when second file file2.csv will arrive then I have to write a code in such a way that if second file data matches in first file data on basis of year and month columns then delete the data from file1 dataframe as it is old data and insert new file2 data in file1 dataframe我有两个文件,一个是 file1.csv,另一个是 file2.csv 我已经将 file1 数据放在一个 dataframe 中,当第二个文件 file2.csv 到达时,我必须以这样的方式编写代码,如果第二个文件数据匹配在基于年份和月份列的第一个文件数据中,然后从文件 1 dataframe 中删除数据,因为它是旧数据,并在文件 1 dataframe 中插入新的文件 2 数据

File1.csv文件1.csv

year month Amount年月金额

2022 Aug 12 2022 年 8 月 12 日

2022 Oct 10 2022 年 10 月 10 日

2021 Jan 20 2021 年 1 月 20 日

2020 March 30 2020年3月30日

file2.csv文件2.csv

year month Amount年月金额

2022 Jan 220 2022 年 1 月 220 日

2022 Feb 130 2022 年 2 月 130

2022 Oct 100 2022 年 10 月 100

final output最后 output

year month Amount年月金额

2022 Aug 12 2022 年 8 月 12 日

2022 Oct 100 2022 年 10 月 100

2021 Jan 20 2021 年 1 月 20 日

2020 March 30 2020年3月30日

2022 Feb 130 2022 年 2 月 130

2022 Jan 220 2022 年 1 月 220 日

I have been trying if exists condition in pyspark but it is not working我一直在尝试 if exists condition in pyspark 但它不起作用

Here are my 2 cents:这是我的 2 美分:

  1. Create 2 dataframes from 2 CSV files(in my case I'm just creating with static data)从 2 个 CSV 文件创建 2 个数据帧(在我的例子中,我只是用 static 数据创建)

     from pyspark.sql.functions import * from pyspark.sql.window import * data1 = [ (2022, 'Aug', 12), (2022, 'Oct', 10), (2021, 'Jan', 20), (2020, 'March', 30)] data2 = [ (2022, 'Jan', 220), (2022, 'Feb', 130), (2022, 'Oct', 100)] df_main = spark.createDataFrame(data1,schema = ['year', 'month', 'Amount']) df_incremental = spark.createDataFrame(data2,schema = ['year', 'month', 'Amount'])
  2. Then Use row_number() on top of year and month and then once evaluated filter only such rows whose row_number is 1, and then drop the row_number column.然后在年份和月份之上使用 row_number(),然后在评估后仅过滤 row_number 为 1 的行,然后删除 row_number 列。

     df_merge = df_incremental.unionAll(df_main) windowSpec = Window.partitionBy('year', 'month').orderBy('year', 'month') df_merge = df_merge.withColumn("_row_number", row_number().over(windowSpec)) df_merge = df_merge.where(df_merge._row_number == 1).drop("_row_number") df_merge.show()

Please find the below image for reference:请找到下图以供参考: 在此处输入图像描述

if second file data matches in first file data on basis of year and month columns then delete the data from file1 dataframe as it is old data and insert new file2 data in file1 dataframe如果第二个文件数据在年和月列的基础上与第一个文件数据匹配,则从 file1 dataframe 中删除数据,因为它是旧数据,并在 file1 dataframe 中插入新的 file2 数据

You can do it with the following steps:您可以按照以下步骤进行操作:

  • do a LEFT join, to assign matching rows from file2.csv DataFrame into file1.csv DataFrame and NULLs if no match is found进行LEFT连接,将文件 2.csv DataFrame 中的匹配行分配到file1.csv file2.csv中,如果未找到匹配项,则为 NULL
  • do a when-otherwise transformation on the year-month pair from file2.csv DataFrame: if columns you want to have in final output are NOT NULL - take these, otherwise, take those from file1.csv DataFrame.file2.csv DataFrame year-month对进行when-otherwise转换:如果您希望在最终 output 中包含的列不是 NULL - 取这些,否则,取file1.csv DataFrame 中的那些。

More on when-otherwise transformation - https://sparkbyexamples.com/pyspark/pyspark-when-otherwise/有关when-otherwise转换的更多信息 - https://sparkbyexamples.com/pyspark/pyspark-when-otherwise/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM