I want to replace the words in description with dataframe from my column and add it to my dataframe

Question:

I want to replace the name and age in description with the data in my table.

data = {'name' : ['Max','Jim'],'Age':[32,44],'desc':''}
desc = "My name is <name> and my age is <age>."

Like this,

Output:
    name    Age desc
0   Max 32  My name is Max and my age is 32.
1   Jim 44  My name is Jim and my age is 44.

I have tried using np.where() and regex functions but still not getting expected results.
I’m using python version 3.11.

Asked By: Rushabh Nalawade

||

Answers:

For a fully programmatic approach, you can use a regex:

import re

data = {'name' : ['Max','Jim'],'Age':[32,44],'desc':''}
desc = "My name is <name> and my age is <age>."

df = pd.DataFrame(data)
df.columns = df.columns.str.lower()

df['desc'] = df.apply(lambda row: re.sub(r'<(w+)>',
                                         lambda m: str(row[m.group(1)]), desc),
                      axis=1)

Or with a list comprehension (assuming unique indices!):

df['desc'] = [re.sub(r'<(w+)>', lambda m: str(df.loc[idx, m.group(1)]), desc)
              for idx in df.index]

Output:

  name  age                              desc
0  Max   32  My name is Max and my age is 32.
1  Jim   44  My name is Jim and my age is 44.

Of course a non-programmatic but much more efficient approach would have been:

df['desc'] = "My name is " + df['name'] + " and my age is " + df['age'] + "."
Answered By: mozway

With single regex replacement for preparing desc (with placeholders), then – just string formatting:

df = pd.DataFrame(data)
df.columns = df.columns.str.lower()
f_desc = re.sub(r'<([^<>]+)>', r'{1}', desc)
df['desc'] = df.apply(lambda r: f_desc.format(**r), axis=1)

  name  age                              desc
0  Max   32  My name is Max and my age is 32.
1  Jim   44  My name is Jim and my age is 44.
Answered By: RomanPerekhrest