在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
- Convert the integer
idto a string to manipulate the prefix. - Use regex to split the string into leading
2s and the remaining characters. - Create a string of
9s matching the length of the leading2s. - 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.
- Group 1: All consecutive leading
- Leading 2s Count: Calculated by subtracting the length of Group 2 from the total length of the
idstring. - Concatenation: We create a string of
9s matching the count of leading2s, 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:
| id | new_id |
|---|---|
| 2222238308750 | 9999938308750 |
| 222222579844 | 999999579844 |
| 222225701296 | 999995701296 |
| 2222250087899 | 9999950087899 |
| 2222237274658 | 9999937274658 |
| 22222955099 | 99999955099 |
| 222285678 | 999985678 |
Notes
- If your
idvalues exceed the 64-bit integer limit (~9e18), replacecast("long")withcast("string")to avoid overflow. - The code leaves
new_ididentical toidfor values that don't start with2.
内容的提问来源于stack exchange,提问作者Scope
相关产品推荐
相关产品推荐

