Apache spark PySpark比较两个数据帧并找到匹配计数
我有2个pyspark数据帧,经过一些操作,每个数据帧包含1列,但两个数据帧的长度不同。dataframe 1是一个配料名称,dataframe 2包含一行长长的配料字符串 数据帧1:Apache spark PySpark比较两个数据帧并找到匹配计数,apache-spark,dataframe,for-loop,pyspark,google-cloud-platform,Apache Spark,Dataframe,For Loop,Pyspark,Google Cloud Platform,我有2个pyspark数据帧,经过一些操作,每个数据帧包含1列,但两个数据帧的长度不同。dataframe 1是一个配料名称,dataframe 2包含一行长长的配料字符串 数据帧1: ingcomb.show(10,truncate=False) +---------------------------------+ |products | +---------------------------------+ |rebel crunch gra
ingcomb.show(10,truncate=False)
+---------------------------------+
|products |
+---------------------------------+
|rebel crunch granola |
|creamed honey |
|mild cheddar with onions & chives|
|berry medley |
|sweet relish made with sea salt |
|spanish peanuts |
|stir fry seasoning mix |
|swiss all natural cheese |
|yellow corn meal |
|shredded wheat |
+---------------------------------+
only showing top 10 rows
数据帧2:
reging.show(10, truncate=30)
+------------------------------+
| ingredients|
+------------------------------+
|apple bean cookie fruit kid...|
|bake bastille day bon appét...|
|dairy fennel gourmet new yo...|
|bon appétit dairy free dinn...|
|bake bon appétit california...|
|bacon basil bon appétit foo...|
|asparagus boil bon appétit ...|
|cocktail party egg fruit go...|
|beef ginger gourmet quick &...|
|dairy free gourmet ham lunc...|
+------------------------------+
only showing top 10 rows
我需要创建一个循环(也欢迎任何其他建议!)来循环数据帧1,通过“like”将值与数据帧字符串进行比较,并给出匹配的总数
预期结果:
+--------------------+-----+
| ingredients|count|
+--------------------+-----+
|rebel crunch granola| 183|
|creamed honey | 87|
|berry medley | 67|
|spanish peanuts | 10|
+--------------------+-----+
我知道以下代码是有效的:
reging.filter("ingredients like '%sugar%'").count()
并试图实现类似于
for i in ingcomb:
x = reging.select("ingredients").filter("ingredients like '%i%'").count()
但不能让PiStac把“i”作为一个值从IcCopy代替字符I.
我已经试过了来自中国的解决方案 但不幸的是,它们不起作用。我在GCP中运行此程序,在尝试运行toPandas时出错-因为权限无法安装pandas 我们实际上可以做一个变通,首先获取数据帧中的计数,然后再与连接匹配。请随时提出更好的建议。新手到这里来编码
counts= reging.select(f.explode("array(Ingredients)").alias('col'))
.groupBy('col').count().orderBy("count", ascending=False)
嗨@ValA你知道这两个数据集大约有多大吗?嗨@AlexandrosBiratsis一个数据框有249245行,另一个有20057行。数据总量略高于1GB,因此根本没有那么大。