当前位置:网站首页>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|
+----+-----+------+
边栏推荐
猜你喜欢
22-08-02 西安 尚医通(02)Vscode、ES6、nodejs、npm、Bable转码器
第六章:存储系统
Example of embedding code for continuous features
Nanny level explains Transformer
在线开启gtid偶发hang住的问题解决
Postman will return to the interface to generate a json file to the local
hashSet解析
商业智能BI业务分析思维:供应链分析 – 如何控制牛鞭效应(二)
Haisi project summary
postman将接口返回结果生成json文件到本地
随机推荐
数据仓库指标体系实践
2022用户画像构建
数仓埋点体系与归因实践
924. 尽量减少恶意软件的传播 前缀和
《多线程案例》阻塞队列、定时器、线程池、饿汉与懒汉模式
pgaudit 的安装使用《postgresql》
Charles capture shows
solution jolt语法
Roson的Qt之旅#103 QML之标签导航控件TabBar
ORB-SLAM2提取特征点
加载properties文件,容器总结
七夕和程序员有毛关系?
DAC、ADC、FFT使用总结
10 分钟彻底理解 Redis 的持久化和主从复制
现货黄金分析的主要流派
计算机网络常见面试题总结
JS作用对象API技巧
Flink的Exactly-Once、状态机制、watermark机制
【RT_Thread学习笔记】---以太网LAN8720A Lwip ping 通网络
C语言版本和GCC版本