当前位置:网站首页>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|
+----+-----+------+
边栏推荐
猜你喜欢
随机推荐
【卫朋】硬件创业:营销与开发同行
开放域OOD主要数据集、评价指标汇总
Data warehouse buried point system and attribution practice
七夕和程序员有毛关系?
ViewModel 记录下 +
word之图表目录中点号位置提升3磅
贷中存量客户的价值挖掘与分类实现,试试这一重要的场景模型
数仓埋点体系与归因实践
Haisi project summary
JS作用对象API技巧
【Shell】3万字图文讲解带你快速掌握shell脚本编程
第四章:架构,Architecture
《多线程案例》阻塞队列、定时器、线程池、饿汉与懒汉模式
The ORB - SLAM2 extracting feature points
一文搞懂什么是@Component和@Bean注解以及如何使用
RHCSA第四天
unity 摄像机旋转拖拽缩放场景
【OpenCV】 - 显示图像API之imshow()对不同位深度(数据类型)的图像的处理方法
mongodb的shell脚本
23届微软秋招内推