Home > other >  Python- Identifying values with a specific format replace one of the elements in these values
Python- Identifying values with a specific format replace one of the elements in these values

Time:07-01

I would like to identify values in a column that have a specific format and then replace one of the elements, the 'x' with a '0'. For instance, the format is " >=3.1.x " or " >=9.5.x ". The desired output is " >=3.1.0 " or " >=9.5.0". How do I replace the x with a 0?

Below is the code I started with:

format_x = re.compile(r"(?P<convert>(?:[>]?=?\d \.\d \.x))")

def convert_format_x(x):
    match = format_x.search(x["column_4"])    
    convert = match.group("convert")
    return
    
testing = testdata['column_4'].apply(convert_constraint, axis = 1)      
 
Output: 
AttributeError: 'NoneType' object has no attribute 'group'

CodePudding user response:

You can use Series.str.replace for the task:

df["column_4"] = df["column_4"].str.replace(
    r"(>=\d \.\d \.)x", r"\g<1>0", regex=True
)

print(df)

Prints:

  column_4
0  >=3.1.0
1  >=9.5.0
2   =x.y.z

Dataframe used:

  column_4
0  >=3.1.x
1  >=9.5.x
2   =x.y.z
  • Related