如何检查pyspark数据框中的字符串列是否都是数字 [英] how to check if a string column in pyspark dataframe is all numeric
本文介绍了如何检查pyspark数据框中的字符串列是否都是数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有 strings
列的 PySpark Dataframe
.如何检查其中的哪些行是数字.我在 PySpark 的 官方文档 -
I have a PySpark Dataframe
with a column of strings
. How can I check which rows in it are Numeric. I could not find any function in PySpark's official documentation -
values = [('25q36',),('75647',),('13864',),('8758K',),('07645',)]
df = sqlContext.createDataFrame(values,['ID',])
df.show()
+-----+
| ID|
+-----+
|25q36|
|75647|
|13864|
|8758K|
|07645|
+-----+
在 Python 中,有一个函数 .isDigit()
如果 string
返回 True
或 False
是否只包含数字.
In Python, there is a function .isDigit()
which returns True
or False
if the string
contains just numbers or not.
预期数据帧 -
+-----+-------+
| ID| Value |
+-----+-------+
|25q36| False |
|75647| True |
|13864| True |
|8758K| False |
|07645| True |
+-----+-------+
我想避免创建UDF
.
推荐答案
一个简单的演员表就可以做到:
A simple cast would do the job :
from pyspark.sql import functions as F
my_df.select(
"ID",
F.col("ID").cast("int").isNotNull().alias("Value ")
).show()
+-----+------+
| ID|Value |
+-----+------+
|25q36| false|
|75647| true|
|13864| true|
|8758K| false|
|07645| true|
+-----+------+
这篇关于如何检查pyspark数据框中的字符串列是否都是数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文