数据框有 2 列:句子和列表。要求是将 df['sentence'] 中存在于 df['list'] 中的字符串替换为找到|存在的字符串。
from pandas import DataFrame
df = {'list': [['Ford','Mercedes Benz'],['ford','hyundai','toyota'],['tesla'],[]],
'sentence': ['Ford is less expensive than Mercedes Benz' ,'toyota and hyundai mileage is good compared to ford','tesla is an electric car','toyota too has electric cars']
}
df = DataFrame(df,columns= ['list','sentence'])
df['sentence'] 的预期输出是:
Ford|present is less expensive than Mercedes Benz|present
toyota|present and hyundai|present mileage is good compared to ford|present
tesla|present is an electric car
toyota too has electric cars
最佳答案
使用正则表达式替换:
(摘自IPython交互式 session )
In [36]: import re
In [37]: def sub_from_list(row):
...: if row['list']:
...: row['sentence'] = re.sub(r'({})'.format('|'.join(set(row['list']))), r'\1|present', row['s
...: entence'])
...: return row
...:
In [38]: df.apply(sub_from_list, axis=1)
Out[38]:
list sentence
0 [Ford, hyundai] Ford|present is expensive than hyundai|present
1 [ford, hyundai, toyota] toyota|present and hyundai|present mileage is ...
2 [tesla] tesla|present is an electric car
3 [] toyota too has electric cars
关于python - 如果字符串在Python的列表中,如何替换文本中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58247271/