0

pyspark のデータフレームからネストされた列を削除しようとしても機能しません。

これは私のコードのシンペットです:

`from pyspark.sql import functions as F from pyspark.sql.types import IntegerType , BooleanType from pyspark.sql.functions import udf

#simple filter function
@F.udf(returnType=BooleanType())
def my_filter(x):
    if (x != df_new.a.b) :
       return True
    else :
       return False

#df.filter(my_filter('id', 'category')).show()

def drop_col(df, struct_nm, delete_struct_child_col_nm):
#fields_to_keep = filter(lambda x:  x != delete_struct_child_col_nm, df.select("{}.*".format(struct_nm)).columns)
fields_to_keep = filter(lambda x:  my_filter(x) , df.select("{}.*".format(struct_nm)).columns)
fields_to_keep = list(map(lambda x:  "{}.{}".format(struct_nm, x), fields_to_keep))
return df.withColumn(struct_nm, struct(fields_to_keep))

drop_col(df_new, "a", df_new.ab)`

次の行を試してもうまくいかなかったため、UDFを使用しました。この not != 記号は機能せず、チルダも使用しないため

#fields_to_keep = filter(lambda x:  x != delete_struct_child_col_nm,     df.select("{}.*".format

編集:誰かがコメントでスキーマを要求したので、私はそれを提供しています

 root
 |-- a: struct (nullable = false)
 |    |-- rawEntity: string (nullable = true)
 |    |-- entityType: string (nullable = true)
 |    |    |-- element: struct (containsNull = true)
 |    |    |    |-- StoreId: string (nullable = true)
 |    |    |    |-- AppId: string (nullable = true)
 |    |-- Timestamps: array (nullable = true)
 |    |    |-- element: long (containsNull = true)
 |    |-- User: string (nullable = true)
 |    |-- b: array (nullable = true)                //trying to drop this
 |    |    |-- element: string (containsNull = true)
 |    |-- KeywordsFull: array (nullable = true)
 |    |    |-- element: struct (containsNull = true)
 |    |    |    |-- Keywords: string (nullable = true)
 |    |    |-- element: string (containsNull = true)
4

0 に答える 0