想要用Python替换某些值的列
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了想要用Python替换某些值的列相关的知识,希望对你有一定的参考价值。
使用的代码:
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
我希望Wooden
和类似的类别完整,如Woodings
,woods
等。还有列:可能是字符串,例如“木头在地面上”,所需的输出是“木在那里”
答案
这是替换字典中所有子字符串的一种方法。请注意,如果字典的任何值和键发生冲突,顺序可能会变得很重要:
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
另一答案
你可以使用pandas replace
function。在字典中定义要替换的内容并替换新列中的单词:
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
虽然对于使用正则表达式的更复杂的替换,编写函数并使用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'(){}()'.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
以上是关于想要用Python替换某些值的列的主要内容,如果未能解决你的问题,请参考以下文章