Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何替换所有的数字和&引用;由「&引用;在Dataframes spark scala中的列的_Scala_Dataframe_Apache Spark - Fatal编程技术网

如何替换所有的数字和&引用;由「&引用;在Dataframes spark scala中的列的

如何替换所有的数字和&引用;由「&引用;在Dataframes spark scala中的列的,scala,dataframe,apache-spark,Scala,Dataframe,Apache Spark,如何替换Dataframes spark scala中列的所有数字和“.”即所有十进制数字都应替换为“”) 例如:+56.5或-64.83应替换为空字符,即“” 我在跟踪 regexp_replace(col("col1"),"\\+|\\-|\\.|0|1|2|3|4|5|6|7|8|9", "") 还有其他更好的方法吗 谢谢看起来需要十进制数的regexp,可以在问题中添加标签“regex”。 此类regexp可用于: // this is pattern to use val decim

如何替换Dataframes spark scala中列的所有数字和“.”即所有十进制数字都应替换为“”)

例如:
+56.5
-64.83
应替换为空字符,即“”

我在跟踪

regexp_replace(col("col1"),"\\+|\\-|\\.|0|1|2|3|4|5|6|7|8|9", "")
还有其他更好的方法吗


谢谢

看起来需要十进制数的regexp,可以在问题中添加标签“regex”。 此类regexp可用于:

// this is pattern to use
val decimalNumbersPattern = "[-+]?[0-9]+\\.[0-9]+"

val df = Seq("Replaced: +56.5", "Replaced: -64.83", "Remains: 44").toDF()
df
  .select(regexp_replace($"value", decimalNumbersPattern, "").alias("result"))
输出:

+-----------+
|result     |
+-----------+
|Replaced:  |
|Replaced:  |
|Remains: 44|
+-----------+

看起来需要十进制数的regexp,可以在问题中添加标记“regex”。 此类regexp可用于:

// this is pattern to use
val decimalNumbersPattern = "[-+]?[0-9]+\\.[0-9]+"

val df = Seq("Replaced: +56.5", "Replaced: -64.83", "Remains: 44").toDF()
df
  .select(regexp_replace($"value", decimalNumbersPattern, "").alias("result"))
输出:

+-----------+
|result     |
+-----------+
|Replaced:  |
|Replaced:  |
|Remains: 44|
+-----------+
结果是:

+---+------------+------------+
| id|all_digitals|not_decimals|
+---+------------+------------+
|  0|       +56.5|            |
|  1|      -64.83|            |
|  2|    +12.1234|            |
|  3|          13|          13|
|  4|       -10.0|            |
|  5|           2|           2|
|  6|           0|           0|
+---+------------+------------+
结果是:

+---+------------+------------+
| id|all_digitals|not_decimals|
+---+------------+------------+
|  0|       +56.5|            |
|  1|      -64.83|            |
|  2|    +12.1234|            |
|  3|          13|          13|
|  4|       -10.0|            |
|  5|           2|           2|
|  6|           0|           0|
+---+------------+------------+

是否要将所有数字替换为空字符串?还是只有十进制数?例如,+24是否需要替换为“”,还是保留为+24?是否希望所有数字都替换为空字符串?还是只有十进制数?也就是说,+24是否需要替换为“”,还是保持为+24?