使用pyspark将特定单词删除到数据框中

问题描述 投票:2回答:1

我有一个DataFrame

+------+--------------------+-----------------+----
|   id| titulo       |tipo      | formacion       |
+------+--------------------+-----------------+----
|32084|A             | Material | VION00001 TRADE |
|32350|B             | Curso    | CUS11222  LEADER|
|32362|C             | Curso    | ITIN9876  EVALUA|   
|32347|D             | Curso    | CUMPLI VION1234 |      
|32036|E             | Curso    | EVAN1111  INFORM|   

我需要在formacion列中删除以VION | CUS | ITIN | VION | EVAN开头的字符,以便数据框看起来像]

+------+--------------------+-----------------+----
|   id| titulo       |tipo      | formacion       |
+------+--------------------+-----------------+----
|32084|A             | Material |  TRADE          |
|32350|B             | Curso    |  LEADER         |
|32362|C             | Curso    |  EVALUA         |   
|32347|D             | Curso    |  CUMPLI         |      
|32036|E             | Curso    |  INFORM         |  
+------+--------------------+-----------------+----

谢谢您的帮助

pyspark helper delete-row word pyspark-dataframes
1个回答
1
投票

使用split函数将列除以space,然后获取数组的最后一个元素。

  • 来自Spark2.4+使用element_at功能
  • 对于Spark < 2.4使用reverse(split(array))[0]

#using element_at
df.withColumn("formacion",element_at(split(col("formacion"),"\\s"),-1)).show() 

#or using array_index
df.withColumn("formacion",split(col("formacion"),"\\s")[1]).show()

#split reverse and get first index value
df.withColumn("formacion",reverse(split(col("formacion"),"\\s"))[0]).show()

#+-----+--------------+----------+-------------+
#|   id|titulo        |tipo      | formacion   |
#+------+--------------------+-----------------+
#|32084|A             | Material |  TRADE      |
#|32350|B             | Curso    |  LEADER     |
#|32362|C             | Curso    |  EVALUA     |   
#|32347|D             | Curso    |  CUMPLI     |      
#|32036|E             | Curso    |  INFORM     |  
#+-----+--------------+----------+-------------+
© www.soinside.com 2019 - 2024. All rights reserved.