python两个dataframe求差集_spark计算两个DataFrame的差集、交集、合集

spark 计算两个dataframe 的差集、交集、合集,只选择某一列来对比比较好。新建两个 dataframe :

import org.apache.spark.{SparkConf, SparkContext}

import org.apache.spark.sql.SQLContext

def main(args: Array[String]): Unit = {

val conf = new SparkConf().setAppName("TTyb").setMaster("local")

val sc = new SparkContext(conf)

val spark = new SQLContext(sc)

val sentenceDataFrame = spark.createDataFrame(Seq(

(1, "asf"),

(2, "2143"),

(3, "rfds")

)).toDF("label", "sentence")

sentenceDataFrame.show()

val sentenceDataFrame1 = spark.createDataFrame(Seq(

(1, "asf"),

(2, "2143"),

(4, "f8934y")

)).toDF("label", "sentence")

sentenceDataFrame1.show()

}

差集 except

val newDF = sentenceDataFrame1.select("sentence").except(sentenceDataFrame.select("sentence"))

newDF.show()

+——–+

|sentence|

+——–+

|f8934y |

+——–+

交集 intersect

val newDF = sentenceDataFrame1.select("sentence").intersect(sentenceDataFrame.select("sentence"))

newDF.show()

+——–+

|sentence|

+——–+

| asf|

| 2143|

+——–+

合集 union

val newDF = sentenceDataFrame1.select("sentence").union(sentenceDataFrame.select("sentence"))

newDF.show()

+——–+

|sentence|

+——–+

| asf|

| 2143|

| f8934y|

| asf|

| 2143|

| rfds|

+——–+

合集最好去一下重 distinct :

val newDF = sentenceDataFrame1.select("sentence").union(sentenceDataFrame.select("sentence")).distinct()

newDF.show()

+——–+

|sentence|

+——–+

| rfds|

| asf|

| 2143|

| f8934y|

+——–+

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值