1
$\begingroup$

I have a dataframe with two columns:

Name          DATE
Name1         20200126
Name2         20200127
Name#DATE#    20200210

I need to replace all the #DATE# with the data from the DATE column, and get something like this:

Name          
Name1         
Name2         
Name20200210

How can I achieve this? I've tried things like this, without any good result..:

df_merged_tables["Name"].str.replace("#DATE#",merged_tables["DATE"])

Thanks!

$\endgroup$
1
$\begingroup$

Your solution is close

Maybe just needed to add an apply.

Try:

df = pd.DataFrame({"Name":["Name1", "Name2", "Name#DATE#"], "Date":[20200126, 20200127, 20200210]})

df["NewColumn"] = df.apply(lambda row: row["Name"].replace("#DATE#", str(row["Date"])), axis = 1)

Outputs:

enter image description here

$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.