当前位置:网站首页>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|
+----+-----+------+
边栏推荐
猜你喜欢
随机推荐
计算机网络常见面试题总结
The ORB - SLAM2 extracting feature points
unity 摄像机旋转拖拽缩放场景
CISP-PTE Zhenti Demonstration
第一章:ARM公司Cortex-M 系列处理器介绍,第二章:嵌入式软件开发介绍和第三章:Cortex-M3和Cortex-M4处理器的一般介绍
uniapp 请求接口封装
PMP每日一练 | 考试不迷路-8.2(包含敏捷+多选)
阿里云-武林头条-建站小能手争霸赛
[机缘参悟-59]:《素书》-6-安于礼仪[安礼章第六]
IEEE RAL投初稿
HCIP笔记整理 2022/7/20
Laravel 中使用子查询
安全狗云原生安全能力全面亮相全球数字经济大会暨ISC互联网安全大会
information_schema
9月考,如何选择靠谱正规的培训机构?
Charles capture shows
solution 【云原生--Kubernetes】kubectl命令详解
数据库表结构文档 生成工具screw的使用
【云原生--Kubernetes】Pod容器与镜像拉取策略
解决plt.imshow()不显示图片cv2.imshw()不显示图片