'Python Panda Concatenate Row and Constant With lamda and Apply()

I can't figure it out...why it won't work for me.

Given that df:

Product Final Code Local Market Code Import Market Code Is_import
Chair I-^placeholder^-23242 sistrix trixsis 1
Desk I-^placeholder^-43343 sistrix trixsis 0
Sofa I-^placeholder^-5454 gitrix trixsis 0
Table I-^placeholder^-545454 gitrix trixgit 1
Trash Can I-^placeholder^-545344 gitrix trixgit 0
Rocking Chair I-^placeholder^-545454 sistrix trixgit 1

I need to replace the ^placeholder^ with Import Market Code row and add a "g-" prefix to tha code if Is_import = 1, else Local Market Code.

Without the "g-" constant, it is working great:

new_df['Final Code']=new_df.apply(
                  lambda row: 
row['Final Code'].replace('^placeholder^',row['Import Market Code']) if (row['Is_import'] == 1)
else
row['Final Code'].replace('^pubExternalId^',row['Local Market Code'])
            ,axis=1)

but when concatenating "g-" constant, it won't add the constant:


new_df['Final Code']=new_df.apply(
                  lambda row: 
row['Final Code'].replace('^placeholder^',f"g-{row['Import Market Code']}") if (row['Is_import'] == 1)
else
row['Final Code'].replace('^pubExternalId^',row['Local Market Code'])
            ,axis=1)

or in this form:


new_df['Final Code']=new_df.apply(
                  lambda row: 
row['Final Code'].replace('^placeholder^',"g-"+row['Import Market Code']) if (row['Is_import'] == 1)
else
row['Final Code'].replace('^pubExternalId^',row['Local Market Code'])
            ,axis=1)

Any idea why?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source