2024-06-28 15:26:01 发布
网友
我有两个Pyspark数据帧(DF1和DF2)。我想检查DF1中的城市是否在DF2的“城市”列中可用,如果是,则从DF2返回国家名称,并创建一个新的数据帧DF3,其中包含Sl.No、City和country
DF1 Sl.无城市 1A 2 b 2c 4d 5 e
DF2 乡村城市 W a V b,c xd,e
DF3 无城市国家 1 a W 2 b V 3 c V 4dx 5 e X
这可以通过使用explode来实现
import pyspark.sql.functions as F l1 = [(1, 'a', ), (2, 'b', ), (3, 'c'), (4, 'd'), (5, 'e')] df1 = sqlContext.createDataFrame(l1, ['sino','city']) #df1.show() l1 = [('W', ['a'] ), ('V', ['b','c'] ), ('X', ['d', 'e'])] df2 = sqlContext.createDataFrame(l1, ['ctry','cities']) #df2.show() df2 = df2.withColumn('cityName', F.explode('cities')) df3 = df1.join(df2, df1.city == df2.cityName).drop('cities', 'cityName') df3.show() + + + + |sino|city|ctry| + + + + | 1| a| W| | 3| c| V| | 5| e| X| | 2| b| V| | 4| d| X| + + + +
这可以通过使用explode来实现
相关问题 更多 >
编程相关推荐