4Manuals

  • PDF Cloud HOME

Pyspark:加入2个数据框仅从第2个数据框中获取新记录(历史记录) Download

    如何熟悉awsglue和pyspark中的日志 窗口末端的PySpark结构化流触发输出 当两个应用程序同时访问一个localspark会话实例时发生错误。调用o2402.defaultParallelism时发生错误 使用pysprk将多级json转换为数据框 如何从pyspark中的数据框创建顶点和边? 填充嵌套结构的元素 pyspark等效于将常量数组作为列添加到数据框 在pySpark日期框架中创建CreateOrReplaceTempView的另一种方法 从其他表获取数据匹配 Mysqli连接三个表

我有2个数据帧df1和df2。我想要这样的数据框的结果: 1.记录df1的所有记录。 2.仅从df2中获取新记录(df1中不可用的记录) 3.生成此逻辑的新数据框

注意::主键是“ id”。我只想检查ID,而不要检查完整的行。如果df1中没有ID,则仅是df2中的故事。

df1

    +------+-------------+-----+
    |  id  |time         |other|
    +------+-------------+-----+
    |   111|  29-12-2019 |   p1|
    |   222|  29-12-2019 |   p2|
    |   333|  29-12-2019 |   p3|
    +----+-----+-----+---------+

df2

    +------+-------------+-----+
    |  id  |time         |other|
    +------+-------------+-----+
    |   111|  30-12-2019 |   p7|
    |   222|  30-12-2019 |   p8|
    |   444|  30-12-2019 |   p0|
    +----+-----+-----+---------+

结果

+------+-------------+-----+
|  id  |time         |other|
+------+-------------+-----+
|   111|  29-12-2019 |   p1|
|   222|  29-12-2019 |   p2|
|   333|  29-12-2019 |   p3|
|   444|  30-12-2019 |   p0|
+----+-----+-----+---------+

能帮我在pyspark中做到这一点吗?我打算使用join。

1 个答案:

答案 0 :(得分:0)

df1=spark.createDataFrame([(111,'29-12-2019','p1'),(222,'29-12-2019','p2'),(333,'29-12-2019','p3')],['id','time','other'])
df2=spark.createDataFrame([(111,'30-12-2019','p7'),(222,'30-12-2019','p8'),(444,'30-12-2019','p0')],['id','time','other'])

mvv1 = df1.select("id").rdd.flatMap(lambda x: x).collect()
print(mvv1)

[111, 222, 333]

yy=",".join([str(x) for x in mvv1])
df2.registerTempTable("temp_df2")
sqlDF2 = sqlContext.sql("select * from temp_df2 where id not in ("+yy+")")
sqlDF2.show()

+---+----------+-----+
| id|      time|other|
+---+----------+-----+
|444|30-12-2019|   p0|
+---+----------+-----+

df1.union(sqlDF2).show()

+---+----------+-----+
| id|      time|other|
+---+----------+-----+
|111|29-12-2019|   p1|
|222|29-12-2019|   p2|
|333|29-12-2019|   p3|
|444|30-12-2019|   p0|
+---+----------+-----+



Similar searches
    可观察和承诺在Angular中如何工作? “ git merge”-已经更新 Samsung RF217 Refrigerator User Manual RewriteMap以从url末尾选择一个ID,如果匹配则用另一个url末尾替换该url末尾 如何通过Java在BigQuery中创建分区表?