嗨,我想在数据帧值中查找特定术语,然后通过与字典键匹配来替换它们。
数据框:
Search term Application
safe high school trip 1
spring break trips 2
gap year trips 1
我有一个要在字典中替换的单词列表,键是要查找的术语,然后用值替换。
{'high school': ['high-school'],
'spring break': ['spring-break'],
'gap year': ['gap-year']}
创意输出:
Search term Application
safe high-school trip 1
spring-break trips 2
gap-year trips 1
我找不到替换数据帧值中部分字符串的方法,因此目前我将数据帧读取为字符串
with open('df.csv','r',encoding='UTF-8') as f:
s = f.read() + '\n'
然后使用 str.replace 像这样一一替换它们,它可以完成工作但效率低下。
s = str.replace(s, 'gap year', 'gap-year')
如果有办法用“-”替换特定术语中的空格,则无需使用字典
谢谢
首先通过删除列表更改字典,然后Series.replace
使用regex=True
替换子字符串:
d = {'high school': 'high-school',
'spring break': 'spring-break',
'gap year': 'gap-year'}
df['Search term'] = df['Search term'].replace(d, regex=True)
print (df)
Search term Application
0 safe high-school trip 1
1 spring-break trips 2
2 gap-year trips 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句