当前位置:网站首页>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|
+----+-----+------+
边栏推荐
猜你喜欢

第一章:ARM公司Cortex-M 系列处理器介绍,第二章:嵌入式软件开发介绍和第三章:Cortex-M3和Cortex-M4处理器的一般介绍

Postman will return to results generated CSV file to the local interface

RHCSA第四天

【图像边缘检测】基于matlab灰度图像的积累加权边缘检测【含Matlab源码 2010期】

帆软11版本参数联动为null查询全部

【图像去雾】基于matlab暗通道和非均值滤波图像去雾【含Matlab源码 2011期】

keepalived安装部署

训练正常&异常的GAN损失函数loss变化应该是怎么样的

Haisi project summary

华为设备配置BFD状态与接口状态联动
随机推荐
9月考,如何选择靠谱正规的培训机构?
Example of embedding code for continuous features
《21天精通TypeScript-5》类型注解与原始类型
QT信号与槽
Roson的Qt之旅#104 QML Image控件
死锁的成因和对应的解决方案
伦敦银现货市场如何使用多条均线?
SSM整合流程
2022年 SQL 优化大全总结详解
学会可视化大屏布局技巧,让领导都赞不绝口
加速FinOps实践,为企业降本增效
深入理解IO流(第一篇)
REST学习
MySQL日期和时间戳的转换
[机缘参悟-59]:《素书》-6-安于礼仪[安礼章第六]
information_schema
(十四)51单片机——LCD1602实现滚动效果
Multi-Head-Attention principle and code implementation
模型训练前后显卡占用对比、多卡训练GPU占用分析【一文读懂】
postman将接口返回结果生成json文件到本地