2017-06-12 85 views
0

我是Spark-Scala的新手。我正在尝试清理一些数据。我在清理FIRSTNAME和LASTNAME列时遇到了问题。字符串中有数字。如何识别数字并用空字符替换整个字符串。如果一个数字存在于一个字符串中,请将该字符串替换为null - Spark

Consider the following dataframe: 

+---------+--------+ 
|FIRSTNAME|LASTNAME| 
+---------+--------+ 
| Steve| 10 C| 
|  Mark| 9436| 
| Brian| Lara| 
+---------+--------+ 

How do I get this: 

+---------+--------+ 
|FIRSTNAME|LASTNAME| 
+---------+--------+ 
| Steve| null| 
|  Mark| null| 
| Brian| Lara| 
+---------+--------+ 

任何帮助将不胜感激。非常感谢你!

编辑:

scala> df2.withColumn("LASTNAME_TEMP", when(col("LASTNAME").contains("1"), null).otherwise(col("LASTNAME"))).show() 
+---------+--------+-------------+ 
|FIRSTNAME|LASTNAME|LASTNAME_TEMP| 
+---------+--------+-------------+ 
| Steve| 10 C|   null| 
|  Mark| 9436|   9436| 
| Brian| Lara|   Lara| 
+---------+--------+-------------+ 

但上面的代码将只在一个字符串。我宁愿它拿一个字符串列表。例如:

val numList = List("1", "2", "3", "4", "5", "6", "7", "8", "9", "0") 

我宣布上述名单,并运行下面的代码:

scala> df2.filter(col("LASTNAME").isin(numList:_*)).show() 

我得到了以下数据框:

+---------+--------+ 
|FIRSTNAME|LASTNAME| 
+---------+--------+ 
+---------+--------+ 
+0

你到目前为止尝试过什么?执行你写的代码时遇到了什么样的问题? – Dima

回答

3

您可以使用正则表达式与rlike模式匹配:

val df = Seq(
    ("Steve", "10 C"), 
    ("Mark", "9436"), 
    ("Brian", "Lara") 
).toDF(
    "FIRSTNAME", "LASTNAME" 
) 

// Keep original LASTNAME in new column only if it doesn't consist of any digit 
val df2 = df.withColumn("LASTNAMEFIXED", when(! col("LASTNAME").rlike(".*[0-9]+.*"), col("LASTNAME"))) 

+---------+--------+-------------+ 
|FIRSTNAME|LASTNAME|LASTNAMEFIXED| 
+---------+--------+-------------+ 
| Steve| 10 C|   null| 
|  Mark| 9436|   null| 
| Brian| Lara|   Lara| 
+---------+--------+-------------+ 
+0

非常感谢!这非常有用。如果你不介意,可以在上面的代码中解释'rlike(“。* [0-9] +。*”)'的作用。 – ankursg8

+0

'rlike(“。* [0-9] +。*”)'会尝试通过[正则表达式](http://www.regular-expressions.info/)检查列LASTNAME是否与包含at至少一位数字。 '。*'表示0个或多个任意字符,'[0-9] +'表示0到9之间的1个或多个数字。 –

+0

明白了。谢谢!真的很感激它。 – ankursg8

相关问题