You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在PySpark中修改整数列前缀并生成新列用于DataFrame内连接

Solution for Replacing Leading 2s with 9s in PySpark Integer Column

To generate the new_id column by replacing all leading consecutive 2s in the integer column id with the same number of 9s (for use in inner joins), follow this implementation:

Approach

  1. Convert the integer id to a string to manipulate the prefix.
  2. Use regex to split the string into leading 2s and the remaining characters.
  3. Create a string of 9s matching the length of the leading 2s.
  4. Concatenate the 9s string with the remaining characters, then convert back to an integer for efficient join operations.

Code Implementation

from pyspark.sql import functions as F

# Apply transformation to your DataFrame (replace 'df' with your actual DataFrame name)
df = df.withColumn(
    "new_id",
    F.concat(
        # Generate string of 9s with length equal to leading 2s count
        F.lpad("", F.length(F.col("id").cast("string")) - F.length(F.regexp_extract(F.col("id").cast("string"), r"^(2+)(.*)$", 2)), "9"),
        # Extract the part of the string after leading 2s
        F.regexp_extract(F.col("id").cast("string"), r"^(2+)(.*)$", 2)
    ).cast("long")  # Convert back to integer for optimal join performance
)

Explanation

  • Regex Split: The pattern ^(2+)(.*)$ captures two groups:
    • Group 1: All consecutive leading 2s in the string.
    • Group 2: The rest of the string after the leading 2s.
  • Leading 2s Count: Calculated by subtracting the length of Group 2 from the total length of the id string.
  • Concatenation: We create a string of 9s matching the count of leading 2s, then append Group 2 to form the new ID.
  • Casting: Converting to long (64-bit integer) ensures fast inner join operations with other DataFrames.

Example Output

Applying this code to your sample data produces the expected result:

idnew_id
22222383087509999938308750
222222579844999999579844
222225701296999995701296
22222500878999999950087899
22222372746589999937274658
2222295509999999955099
222285678999985678

Notes

  • If your id values exceed the 64-bit integer limit (~9e18), replace cast("long") with cast("string") to avoid overflow.
  • The code leaves new_id identical to id for values that don't start with 2.

内容的提问来源于stack exchange,提问作者Scope

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 22:50:23