【问题标题】:remove specific words into a dataframe with pyspark使用 pyspark 将特定单词删除到数据框中
【发布时间】:2020-05-17 23:30:38
【问题描述】:

我有一个数据框

+------+--------------------+-----------------+----
|   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         |  
+------+--------------------+-----------------+----

感谢您的帮助

【问题讨论】:

    标签: pyspark helper delete-row word pyspark-dataframes


    【解决方案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     |  
    #+-----+--------------+----------+-------------+
    

    【讨论】:

      【解决方案2】:

      对不起,这是来自 DataFrame 的原始列

      formacion = [ VION00001 TRADE, CUS11222 LEADER,ITIN9876 EVALUA ,VION1234 CUMPLI,EVAN11 FR]

      这是意料之中的

      formacion = [ TRADE, LEADER,EVALUA ,CUMPLI, FR]

      【讨论】:

        猜你喜欢
        • 2022-12-29
        • 1970-01-01
        • 2019-07-17
        • 1970-01-01
        • 1970-01-01
        • 2019-05-25
        • 2017-09-22
        • 2018-04-04
        • 1970-01-01
        相关资源
        最近更新 更多