You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何访问DataFrame中JSON字段特定列并过滤空children数组数据?

解决方法

报错原因

你用array('')构造的是字符串数组,但children列的类型是嵌套结构体数组,两者类型完全不匹配,因此触发报错。

正确过滤空数组的方法

要筛选children列不含任何元素的数据,直接判断数组长度为0即可,两种实现方式如下:

1. SQL表达式语法

df.filter("size(children) = 0").show()

2. DataFrame API(需导入函数库)

from pyspark.sql import functions as F

df.filter(F.size(F.col("children")) == 0).show()

额外处理:包含null的情况

如果children列可能存在null值,想要同时过滤null和空数组,可修改条件:

SQL表达式方式

df.filter("children is null or size(children) = 0").show()

DataFrame API方式

df.filter((F.col("children").isNull()) | (F.size(F.col("children")) == 0)).show()

内容的提问来源于stack exchange,提问作者tathagat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 21:51:16