replace a word in a column with values from another column in pandas dataframe
Question:
In below table, I want to create a new column or update the Comment column that can replace the placeholder word "NameTag" with the value in column "Name" and the word "IDTag" with the value in column "ID"
ID
Name
Comment
A1
Alex
the name NameTag belonging to IDTag is a common name
A2
Alice
Judging the NameTag of IDTag, I feel its a girl’s name
I want the output like this…
ID
Name
Comment
A1
Alex
the name Alex belonging to A1 is a common name
A2
Alice
Judging the Alice of A2, I feel its a girl’s name
I tried pandas replace function but it didn’t work to replace word with column name… getting below error
ValueError: Series.replace cannot use dict-value and non-None to_replace
Answers:
Using .apply and lambda:
df["Comment"] = df.apply(lambda x: x["Comment"].replace("NameTag", x["Name"]), axis=1)
import pandas as pd
data = {'ID': ['A1', 'A2'], 'Name': ['Alex', 'Alice'], 'Comment': ['the name NameTag belonging to IDTag is a common name', 'Judging the NameTag of IDTag, I feel its a girl's name']}
df = pd.DataFrame(data)
df['Personalized'] = df.Comment.replace(['NameTag'] * len(df), df.Name, regex=True)
print(df.Personalized)
prints
0 the name Alex belonging to IDTag is a common name
1 Judging the Alex of IDTag, I feel its a girl's...
Use list comprehension:
df["Comment"] = [c.replace("NameTag", n).replace("IDTag", i)
for c, n, i in zip(df['Comment'], df['Name'], df['ID'])]
print (df)
ID Name Comment
0 A1 Alex the name Alex belonging to A1 is a common name
1 A2 Alice Judging the Alice of A2 , I feel its a girl's...
given the duplicated dataFrame "test":
test = pd.DataFrame({
'ID': ['A1', 'A2'],
'Name': ['Alex', 'Alice'],
'Comment': ["the name NameTag belonging to IDTag is a common name", "Judging the NameTag of IDTag, I feel its a girl's name"]})
test
output:
ID Name Comment
0 A1 Alex the name NameTag belonging to IDTag is a commo...
1 A2 Alice Judging the NameTag of IDTag, I feel its a gir...
This for loop does what you need I think,
for row in range(test.shape[0]):
name = test['Name'].loc[row]
id = test['ID'].loc[row]
test['Comment'].loc[row] = test['Comment'].loc[row].replace('NameTag', name)
test['Comment'].loc[row] = test['Comment'].loc[row].replace('IDTag', id)
test
output:
ID Name Comment
0 A1 Alex the name Alex belonging to A1 is a common name
1 A2 Alice Judging the Alice of A2, I feel its a girl's name
For a generic method that works with any number of columns you can use a regex and a list comprehension:
import re
cols = ['ID', 'Name']
pattern = '|'.join([f'{x}Tag' for x in cols]) # 'IDTag|NameTag'
r = re.compile(fr"b({pattern})b")
df['Comment'] = [r.sub(lambda m: d.get(m.group(1)), s)
for d, s in zip(df[cols].add_suffix('Tag').to_dict('index').values(),
df['Comment'])]
Output:
ID Name Comment
0 A1 Alex the name Alex belonging to A1 is a common name
1 A2 Alice Judging the Alice of A2, I feel its a girl's name
try :
import pandas as pd
df = pd.DataFrame({
'ID':['A1','A2'],
'Name':['Alex','Alice'],
'Comment':["the name NameTag belonging to IDTag is a common name","Judging the NameTag of IDTag, I feel its a girl's name"]
})
for i in range(len(df['Comment'])):
df.loc[i,'Comment'] = df.loc[i,'Comment'].replace('IDTag',df.loc[i,'ID']).replace('NameTag',df.loc[i,'Name'])
In below table, I want to create a new column or update the Comment column that can replace the placeholder word "NameTag" with the value in column "Name" and the word "IDTag" with the value in column "ID"
ID | Name | Comment |
---|---|---|
A1 | Alex | the name NameTag belonging to IDTag is a common name |
A2 | Alice | Judging the NameTag of IDTag, I feel its a girl’s name |
I want the output like this…
ID | Name | Comment |
---|---|---|
A1 | Alex | the name Alex belonging to A1 is a common name |
A2 | Alice | Judging the Alice of A2, I feel its a girl’s name |
I tried pandas replace function but it didn’t work to replace word with column name… getting below error
ValueError: Series.replace cannot use dict-value and non-None to_replace
Using .apply and lambda:
df["Comment"] = df.apply(lambda x: x["Comment"].replace("NameTag", x["Name"]), axis=1)
import pandas as pd
data = {'ID': ['A1', 'A2'], 'Name': ['Alex', 'Alice'], 'Comment': ['the name NameTag belonging to IDTag is a common name', 'Judging the NameTag of IDTag, I feel its a girl's name']}
df = pd.DataFrame(data)
df['Personalized'] = df.Comment.replace(['NameTag'] * len(df), df.Name, regex=True)
print(df.Personalized)
prints
0 the name Alex belonging to IDTag is a common name
1 Judging the Alex of IDTag, I feel its a girl's...
Use list comprehension:
df["Comment"] = [c.replace("NameTag", n).replace("IDTag", i)
for c, n, i in zip(df['Comment'], df['Name'], df['ID'])]
print (df)
ID Name Comment
0 A1 Alex the name Alex belonging to A1 is a common name
1 A2 Alice Judging the Alice of A2 , I feel its a girl's...
given the duplicated dataFrame "test":
test = pd.DataFrame({
'ID': ['A1', 'A2'],
'Name': ['Alex', 'Alice'],
'Comment': ["the name NameTag belonging to IDTag is a common name", "Judging the NameTag of IDTag, I feel its a girl's name"]})
test
output:
ID Name Comment
0 A1 Alex the name NameTag belonging to IDTag is a commo...
1 A2 Alice Judging the NameTag of IDTag, I feel its a gir...
This for loop does what you need I think,
for row in range(test.shape[0]):
name = test['Name'].loc[row]
id = test['ID'].loc[row]
test['Comment'].loc[row] = test['Comment'].loc[row].replace('NameTag', name)
test['Comment'].loc[row] = test['Comment'].loc[row].replace('IDTag', id)
test
output:
ID Name Comment
0 A1 Alex the name Alex belonging to A1 is a common name
1 A2 Alice Judging the Alice of A2, I feel its a girl's name
For a generic method that works with any number of columns you can use a regex and a list comprehension:
import re
cols = ['ID', 'Name']
pattern = '|'.join([f'{x}Tag' for x in cols]) # 'IDTag|NameTag'
r = re.compile(fr"b({pattern})b")
df['Comment'] = [r.sub(lambda m: d.get(m.group(1)), s)
for d, s in zip(df[cols].add_suffix('Tag').to_dict('index').values(),
df['Comment'])]
Output:
ID Name Comment
0 A1 Alex the name Alex belonging to A1 is a common name
1 A2 Alice Judging the Alice of A2, I feel its a girl's name
try :
import pandas as pd
df = pd.DataFrame({
'ID':['A1','A2'],
'Name':['Alex','Alice'],
'Comment':["the name NameTag belonging to IDTag is a common name","Judging the NameTag of IDTag, I feel its a girl's name"]
})
for i in range(len(df['Comment'])):
df.loc[i,'Comment'] = df.loc[i,'Comment'].replace('IDTag',df.loc[i,'ID']).replace('NameTag',df.loc[i,'Name'])