gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:03:10 27 4
gpt4 key购买 nike

我有一个数据框

+------+--------------------+-----------------+----
| 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 开头的字符,这样 Dataframe 看起来像

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

谢谢你的帮助

最佳答案

使用 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 |
#+-----+--------------+----------+-------------+

关于pyspark - 使用 pyspark 将特定单词删除到数据框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61860152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com