pyspark dataframe-filter嵌套列

w8ntj3qf  于 2021-07-12  发布在  Spark
关注(0)|答案(1)|浏览(317)

我知道有很多类似的问题,但我还没有找到任何符合我的情况下,所以请不要太高兴与重复标志。我正在用spark 3.0.1在azure databricks中使用Python3笔记本。
我有以下Dataframe

+---+---------+--------+
|ID |FirstName|LastName|
+---+---------+--------+
|1  |John     |Doe     |
|2  |Michael  |        |
|3  |Angela   |Merkel  |
+---+---------+--------+

可以用这个代码创建

from pyspark.sql.types import StructType,StructField, StringType, IntegerType
import pyspark.sql.functions as F

data2 = [(1,"John","Doe"),
    (2,"Michael",""),
    (3,"Angela","Merkel")
  ]

schema = StructType([ \
    StructField("ID",IntegerType(),True), \
    StructField("FirstName",StringType(),True), \
    StructField("LastName",StringType(),True), \
  ])

df1 = spark.createDataFrame(data=data2,schema=schema)
df1.printSchema()
df1.show(truncate=False)

我把它转换成这个Dataframe

+---+-----------------------------------------+
|ID |Names                                    |
+---+-----------------------------------------+
|1  |[[FirstName, John], [LastName, Doe]]     |
|2  |[[FirstName, Michael], [LastName, ]]     |
|3  |[[FirstName, Angela], [LastName, Merkel]]|
+---+-----------------------------------------+

使用此代码

df2 = df1.select(
            'ID', 
            F.array(
                F.struct(
                    F.lit('FirstName').alias('NameType'), 
                    F.col('FirstName').alias('Name')
                ), 
                F.struct(
                    F.lit('LastName').alias('NameType'), 
                    F.col('LastName').alias('Name')
                )
            ).alias('Names')
        )

df2.printSchema()
df2.show(truncate=False)

现在,我想过滤掉 Names 在哪里 LastName 为null或为空字符串。我的总体目标是拥有一个可以在json中序列化的对象,其中 Names 一个空的 Name 值被排除在外。
这样地

[
    {
        "ID": 1,
        "Names": [
            {
                "NameType": "FirstName",
                "Name": "John"
            },
            {
                "NameType": "LastName",
                "Name": "Doe"
            }
        ]
    },
    {
        "ID": 2,
        "Names": [
            {
                "NameType": "FirstName",
                "Name": "Michael"
            }
        ]
    },
    {
        "ID": 3,
        "Names": [
            {
                "NameType": "FirstName",
                "Name": "Angela"
            },
            {
                "NameType": "LastName",
                "Name": "Merkel"
            }
        ]
    }
]

我试过了

df2 = df1.select(
            'ID', 
            F.array(
                F.struct(
                    F.lit('FirstName').alias('NameType'), 
                    F.col('FirstName').alias('Name')
                ), 
                F.struct(
                    F.lit('LastName').alias('NameType'), 
                    F.col('LastName').alias('Name')
                )
            ).filter(lambda x: x.col('LastName').isNotNull()).alias('Names')
        )

但我得到了错误 'Column' object is not callable .
我也试过了 df2 = df2.filter(F.col('Names')['LastName']) > 0) 但这给了我一个机会 invalid syntax 错误。
我试过了

df2 = df2.filter(lambda x: (len(x)>0), F.col('Names')['LastName'])

但这就是错误 TypeError: filter() takes 2 positional arguments but 3 were given .
有人能告诉我怎么做吗?

093gszye

093gszye1#

你可以使用高阶函数 filter :

import pyspark.sql.functions as F

df3 = df2.withColumn(
    'Names', 
    F.expr("filter(Names, x -> case when x.NameType = 'LastName' and length(x.Name) = 0 then false else true end)")
)

df3.show(truncate=False)
+---+-----------------------------------------+
|ID |Names                                    |
+---+-----------------------------------------+
|1  |[[FirstName, John], [LastName, Doe]]     |
|2  |[[FirstName, Michael]]                   |
|3  |[[FirstName, Angela], [LastName, Merkel]]|
+---+-----------------------------------------+

相关问题