要用特定值替换Python中的列吗
使用的代码:要用特定值替换Python中的列吗,python,python-3.x,pandas,dataframe,replace,Python,Python 3.x,Pandas,Dataframe,Replace,使用的代码: def fn(x): for i in x: x=x.replace('Wood','Wooden') return x test['Coming:'] = test['Column:'].apply(fn) 样本输出: Column: Coming: Needed: Wood Wooden Wooden
def fn(x):
for i in x:
x=x.replace('Wood','Wooden')
return x
test['Coming:'] = test['Column:'].apply(fn)
样本输出:
Column: Coming: Needed:
Wood Wooden Wooden
Wooden Woodenen Wooden
我希望木质
和类似的类别保持完整,如木质
,木质
等。。
此外,列:可以是字符串,例如“Wood is the ground”,需要的输出是“Wood is the ground”您可以使用。在字典中定义要替换的内容,并替换新列中的单词:
import pandas as pd
#test data
df = pd.DataFrame(["Wood", "Wooden", "Woody Woodpecker", "wood", "wool", "wool suit"], columns = ["old"])
#dictionary for substitutions
subst_dict = {"Wood": "Wooden", "wool": "soft"}
df["new"] = df["old"].replace(subst_dict)
#output
old new
0 Wood Wooden
1 Wooden Wooden
2 Woody Woodpecker Woody Woodpecker
3 wood wood
4 wool soft
5 wool suit wool suit
尽管对于使用regex的更复杂的替换,编写一个函数并使用apply()
方法可能是一个好主意
更改要求后更新:如果只想匹配短语中的整词,可以使用正则表达式:
import pandas as pd
#test data
df = pd.DataFrame(["Wood", "Wooden", "Woody Woodpecker", "wood", "wool", "wool suit", "Wood is delicious", "A beautiful wool suit"], columns = ["old"])
#dictionary for substitutions
subst_dict = {"Wood": "Wooden", "wool": "soft"}
#create dictionary of regex expressions
temp_dict = {r'(\b){}(\b)'.format(k) : v for k, v in subst_dict.items()}
#and substitute
df["new"] = df["old"].replace(temp_dict, regex = True)
#output
old new
0 Wood Wooden
1 Wooden Wooden
2 Woody Woodpecker Woody Woodpecker
3 wood wood
4 wool soft
5 wool suit soft suit
6 Wood is delicious Wooden is delicious
7 A beautiful wool suit A beautiful soft suit
你可以用。在字典中定义要替换的内容,并替换新列中的单词:
import pandas as pd
#test data
df = pd.DataFrame(["Wood", "Wooden", "Woody Woodpecker", "wood", "wool", "wool suit"], columns = ["old"])
#dictionary for substitutions
subst_dict = {"Wood": "Wooden", "wool": "soft"}
df["new"] = df["old"].replace(subst_dict)
#output
old new
0 Wood Wooden
1 Wooden Wooden
2 Woody Woodpecker Woody Woodpecker
3 wood wood
4 wool soft
5 wool suit wool suit
尽管对于使用regex的更复杂的替换,编写一个函数并使用apply()
方法可能是一个好主意
更改要求后更新:如果只想匹配短语中的整词,可以使用正则表达式:
import pandas as pd
#test data
df = pd.DataFrame(["Wood", "Wooden", "Woody Woodpecker", "wood", "wool", "wool suit", "Wood is delicious", "A beautiful wool suit"], columns = ["old"])
#dictionary for substitutions
subst_dict = {"Wood": "Wooden", "wool": "soft"}
#create dictionary of regex expressions
temp_dict = {r'(\b){}(\b)'.format(k) : v for k, v in subst_dict.items()}
#and substitute
df["new"] = df["old"].replace(temp_dict, regex = True)
#output
old new
0 Wood Wooden
1 Wooden Wooden
2 Woody Woodpecker Woody Woodpecker
3 wood wood
4 wool soft
5 wool suit soft suit
6 Wood is delicious Wooden is delicious
7 A beautiful wool suit A beautiful soft suit
下面是替换字典中所有子字符串的一种方法。请注意,如果字典中的任何值和键发生冲突,顺序可能会变得很重要:
import pandas as pd
s = pd.Series(['Wood', 'Wooden', 'Woody Woodpecker', 'wood', 'wood', 'wool suit'])
d = {'Wood': 'Wooden', 'wool': 'soft'}
for k, v in d.items():
s = s.str.replace(k, v)
# 0 Wooden
# 1 Woodenen
# 2 Woodeny Woodenpecker
# 3 wood
# 4 wood
# 5 soft suit
# dtype: object
下面是替换字典中所有子字符串的一种方法。请注意,如果字典中的任何值和键发生冲突,顺序可能会变得很重要:
import pandas as pd
s = pd.Series(['Wood', 'Wooden', 'Woody Woodpecker', 'wood', 'wood', 'wool suit'])
d = {'Wood': 'Wooden', 'wool': 'soft'}
for k, v in d.items():
s = s.str.replace(k, v)
# 0 Wooden
# 1 Woodenen
# 2 Woodeny Woodenpecker
# 3 wood
# 4 wood
# 5 soft suit
# dtype: object
但它对字符串不起作用。e、 g.如果old=“Wood is there in the garden”我想要的是“Wood is there in the garden”,请帮助解决它的紧迫性。请不要通过添加新规则来改变您的问题。有人建议问一个新问题,但这对字符串不起作用。e、 g.如果old=“Wood is there in the garden”我想要的是“Wood is there in the garden”,请帮助解决它的紧迫性。请不要通过添加新规则来改变您的问题。有人建议问一个新问题。关于词典顺序的尖锐评论。但是OP问题甚至没有提到字典。我只是在我的回答中介绍了它,因为我认为可能需要替换多个单词。@MrT,我努力找到一种矢量化的方法。我希望有一个是存在的,因为这是一个循环。我希望木头是完整的,但它正在转化为“Woodenen”,你能提出一个新的问题吗?这远远超出了你最初的要求。这样你就可以清楚地告诉我们你希望应用的规则。关于字典顺序的尖锐评论。但是OP问题甚至没有提到字典。我只是在我的回答中介绍了它,因为我认为可能需要替换多个单词。@MrT,我努力找到一种矢量化的方法。我希望有一个是存在的,因为这是一个循环。我希望木头是完整的,但它正在转化为“Woodenen”,你能提出一个新的问题吗?这远远超出了你最初的要求。这样,您就可以清楚地告诉我们您希望应用的规则。