当前位置:网站首页>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|
+----+-----+------+
边栏推荐
猜你喜欢
随机推荐
hashSet解析
idea远程debug
JS 原型原型链
模型训练前后显卡占用对比、多卡训练GPU占用分析【一文读懂】
第四章:架构,Architecture
多线程打印ABC(继承+进阶)
力扣解法汇总622-设计循环队列
C语言实现树的底层遍历--超简代码
How to choose a reliable and formal training institution for the exam in September?
多线程可见
薛定谔的对象属性判断
《21天精通TypeScript-5》类型注解与原始类型
pt-online-schema-change工具使用的一次
【云原生--Kubernetes】Pod重启策略
MySQL - 触发器
请手撸5种常见限流算法!面试必备
Postman will return to the interface to generate a json file to the local
volatile
【图像去雾】基于matlab暗通道和非均值滤波图像去雾【含Matlab源码 2011期】
MySQL - 视图操作









