当前位置:网站首页>Pyspark - an empty string is replaced by None
Pyspark - an empty string is replaced by None
2022-08-03 07:43: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|
+----+-----+------+
边栏推荐
猜你喜欢
随机推荐
idea远程debug
线程基础(二)
【图像边缘检测】基于matlab灰度图像的积累加权边缘检测【含Matlab源码 2010期】
力扣解法汇总622-设计循环队列
一文搞懂什么是@Component和@Bean注解以及如何使用
在线开启gtid偶发hang住的问题解决
华为设备配置BFD单跳检测二层链路
酷雷曼上新6大功能,全景营销持续加码
数据仓库指标体系实践
分治法求解中位数
Week5
分布式数据库数据一致性的原理、与技术实现方案
Flink对比Spark
uniapp 请求接口封装
数仓埋点体系与归因实践
神经网络原理及代码实现
【C语言】函数栈帧的创建和销毁详解
LiveData 记录下 +
Roson的Qt之旅#103 QML之标签导航控件TabBar
9月考,如何选择靠谱正规的培训机构?









