0

使用此代码删除带有 pyspark 的嵌套列时出现此错误。为什么这不起作用?正如错误所暗示的那样,我试图使用波浪号而不是 not != 但它也不起作用。那么在这种情况下你会怎么做呢?

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 = list(map(lambda x:  "{}.{}".format(struct_nm, x), fields_to_keep))
    return df.withColumn(struct_nm, struct(fields_to_keep))
4

1 回答 1

2

我用一个结构列和几个虚拟列构建了一个简单的例子:

from pyspark import SQLContext
from pyspark.sql import SparkSession
from pyspark.sql.functions import monotonically_increasing_id, lit, col, struct
from pyspark.sql.types import StructType, StructField, StringType, IntegerType

spark = SparkSession.builder.getOrCreate()
sql_context = SQLContext(spark.sparkContext)
schema = StructType(
    [
        StructField('addresses',
                    StructType(
                        [StructField("state", StringType(), True),
                         StructField("street", StringType(), True),
                        StructField("country", StringType(), True),
                         StructField("code", IntegerType(), True)]
                    )
                    )
    ]
)

rdd = [({'state': 'pa', 'street': 'market', 'country': 'USA', 'code': 100},),
       ({'state': 'ca', 'street': 'baker',  'country': 'USA', 'code': 101},)]

df = sql_context.createDataFrame(rdd, schema)
df = df.withColumn('id', monotonically_increasing_id())
df = df.withColumn('name', lit('test'))

print(df.show())
print(df.printSchema())

输出:

+--------------------+-----------+----+
|           addresses|         id|name|
+--------------------+-----------+----+
|[pa, market, USA,...| 8589934592|test|
|[ca, baker, USA, ...|25769803776|test|
+--------------------+-----------+----+

root
 |-- addresses: struct (nullable = true)
 |    |-- state: string (nullable = true)
 |    |-- street: string (nullable = true)
 |    |-- country: string (nullable = true)
 |    |-- code: integer (nullable = true)
 |-- id: long (nullable = false)
 |-- name: string (nullable = false)

要删除整个 struct 列,您可以简单地使用以下drop函数:

df2 = df.drop('addresses')
print(df2.show())

输出:

+-----------+----+
|         id|name|
+-----------+----+
| 8589934592|test|
|25769803776|test|
+-----------+----+

要删除特定字段,在结构列中,它有点复杂 - 这里还有一些其他类似的问题:

无论如何,我发现它们有点复杂——我的方法就是用你想要保留的结构字段的子集重新分配原始列:

columns_to_keep = ['country', 'code']

df = df.withColumn('addresses', struct(*[f"addresses.{column}" for column in columns_to_keep]))

输出:

+----------+-----------+----+
| addresses|         id|name|
+----------+-----------+----+
|[USA, 100]| 8589934592|test|
|[USA, 101]|25769803776|test|
+----------+-----------+----+

或者,如果您只想指定要删除的列而不是要保留的列:

columns_to_remove = ['country', 'code']
all_columns = df.select("addresses.*").columns
columns_to_keep = list(set(all_columns) - set(columns_to_remove))
df = df.withColumn('addresses', struct(*[f"addresses.{column}" for column in columns_to_keep]))

输出:

+------------+-----------+----+
|   addresses|         id|name|
+------------+-----------+----+
|[pa, market]| 8589934592|test|
| [ca, baker]|25769803776|test|
+------------+-----------+----+

希望这可以帮助!

于 2020-04-10T12:19:17.423 回答