我已经定义了一个函数,该函数返回作为输入给出的所有数据帧的交集的数据帧。但是,当我将函数的输出存储在某个变量中时,它不会存储在该变量中。显示为无类型对象
def intersection(list1, intersection_df,i):
if (i == 1):
intersection_df = list1[0]
print(type(intersection_df))
intersection(list1, intersection_df, i+1)
elif (i>len(list1)):
print(type(intersection_df))
a = spark.createDataFrame(intersection_df.rdd)
a.show()
return a
else:
intersection_df = intersection_df.alias('intersection_df')
tb = list1[i-1]
tb = tb.alias('tb')
intersection_df = intersection_df.join(tb, intersection_df['value'] == tb['value']).where(col('tb.value').isNotNull()).select(['intersection_df.value'])
print(type(intersection_df))
intersection(list1, intersection_df, i+1)
例如,如果我输入如下信息,
list1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
list2 = [3,4,5,6,7,8,9,10,11,12,13,14,15,16]
list3 = [6,7,8,9,10,11,12,13,4,16,343]
df1 = spark.createDataFrame(list1, StringType())
df2 = spark.createDataFrame(list2, StringType())
df3 = spark.createDataFrame(list3, StringType())
list4 = [df1,df2,df3]
empty_df = []
intersection_df = intersection(list4, empty_df, 1)
我希望以下输出存储在interesection_df
中 +-----+
|value|
+-----+
| 7 |
| 11 |
| 8 |
| 6 |
| 9 |
| 10 |
| 4 |
| 12 |
| 13 |
+-----+
答案 0 :(得分:0)
我认为您被递归的诅咒所打击。
问题:
您递归调用intersection
,但仅在if条件之一中返回。因此,当它返回您的df时,它无处可去(回想一下:每个函数调用都会创建一个堆栈)。
解决方案:
当您从intersection
和if
条件中调用else
时返回。您的return intersection(list1, intersection_df, i+1)
条件下的if
。