当前位置:网站首页>pyspark --- 空串替换为None
pyspark --- 空串替换为None
2022-08-03 05:29:00 【WGS.】
df = ss.createDataFrame([{
'time_h': '', 'city': '北京', 'model': '华为'},
{
'time_h': '', 'city': '北京', 'model': '华为'},
{
'time_h': '', 'city': '', 'model': ''},
{
'time_h': None, 'city': None, 'model': None},
{
'time_h': None, 'city': '青岛', 'model': '华为'},
{
'time_h': '20', 'city': '青岛', 'model': '华为'}])\
.select(*['city', 'model', 'time_h'])
df.show()
# # 方法1
# for column in df.columns:
# trimmed = fn.trim(fn.col(column))
# df = df.withColumn(column, fn.when(fn.length(trimmed) != 0, trimmed).otherwise(None))
# 方法2
df = df.replace(to_replace='', value=None, subset=['time_h', 'model', 'city'])
df.show()
+----+-----+------+
|city|model|time_h|
+----+-----+------+
|北京| 华为| |
|北京| 华为| |
| | | |
|null| null| null|
|青岛| 华为| null|
|青岛| 华为| 20|
+----+-----+------+
+----+-----+------+
|city|model|time_h|
+----+-----+------+
|北京| 华为| null|
|北京| 华为| null|
|null| null| null|
|null| null| null|
|青岛| 华为| null|
|青岛| 华为| 20|
+----+-----+------+
边栏推荐
猜你喜欢

5G网络入门基础--5G网络的架构与基本原理

MySql之json_extract函数处理json字段

MySQL的on duplicate key update 的使用

使用Contab调用Shell脚本执行expdp自动备份Oracle

Use of Alibaba Cloud SMS Service (create, test notes)

【云原生 · Kubernetes】Kubernetes基础环境搭建

PCB 多层板为什么都是偶数层?

Shell脚本--信号发送与捕捉

【multi_scale】多尺度训练——目标检测训练trick

SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
随机推荐
配置MSTP功能示例
2021年PHP-Laravel面试题问卷题 答案记录
【Personal summary】Key points of MES system development/management
【应届生租房】应届生如何租房以及注意事项
UniApp scroll-view 事件不生效(@scroll、@scrolltolower、@scrolltoupper ...)
一家可靠的HDI板厂,需要具备哪些基本条件?
el-tree设置利用setCheckedNodessetCheckedKeys默认勾选节点,以及通过setChecked新增勾选指定节点
Charles抓包显示<unknown>解决方案
Monitoring Oracle11gR2 in Zabbix6.0 of OracleLinux8.6
【云原生 · Kubernetes】搭建Harbor仓库
Composer require 报错 Installation failed, reverting ./composer.json and ./composer.lock to their ...
el-table实现列筛选功能,控制列的显示和隐藏(实现简单,效果满分)
一文读懂PCB品质体系认证
ESXI主机给虚拟机添加USB加密狗设备
ES6中 Symbol 的基础学习,迭代器和生成器的基本用法
IDEA连接mysql又报错!Server returns invalid timezone. Go to ‘Advanced‘ tab and set ‘serverTimezone‘ prope
MySQL的主从复制
ESXI中损坏虚拟机数据如何找回
Content type ‘applicationx-www-form-urlencoded;charset=UTF-8‘ not supported“【已解决】
VO、DTO、DO、POJO的区别和概念