我正試圖根據'%'符號來分割列的資料。但我有一些資料不包含'%'符號。
輸入資料
|Default_value|
-------------------
| 10% OF VALUE|
| 20% OF VALUE|
| 這個 是 null VALUE|
| 0 is the value|
-------------------
預期輸出
|value | Description|
------------------- -------------------
| 10%| OF VALUE |
| 20%| OF VALUE !
|這個是null VALUE |。
| 0 是的值 |
------------------- -------------------
我試著在'%'上使用regex,但是沒有'%'的行在'value'列下出現了 而我想把它放在'描述'欄里。
uj5u.com熱心網友回復:
你可以使用regexp_extract函式
df = spark. createDataFrame(['10% OF VALUE', '20% OF VALUE', 'this is null VALUE', '0 is the value'], StringType()
.toDF('Default_value')
df.withColumn('value', regexp_extract('Default_value', '.*%', 0)
.withColumn('Description', regexp_extract('Default_value', '(.*%|.{0})(.*)', 2) ) .show()
------------------ ----- ------------------
|默認值|值|描述|
------------------ ----- ------------------
| 10% OF VALUE| 10%| OF VALUE| >。
20% OF VALUE| 20%| OF VALUE|
|這個是 null VALUE| |這個是 null VALUE|
| 0 is the value| | 0 is the value| >。
------------------ ----- ------------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/307541.html
標籤:
