python - 如果字符串在Python的列表中,如何替换文本中的字符串?

标签 python pandas list dataframe replace

数据框有 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/

相关文章:

python - 将 pandas 条形图的图例与次要 y 轴放在条形图前面

python - Pandas DataFrames 中的平等 - 列顺序很重要?

algorithm - 从链表中有效地选择一组随机元素

Python - 字典列表中的值

python - 确保文件 move 结束,然后由其他程序复制

python - 无法在 Google CloudSQL 上运行syncdb 操作错误 1045 使用密码 : NO 的用户 'root' @'localhost' 的访问被拒绝

python - Matplotlib 在所有子图上显示 x-ticks 和唯一的 y 标签

python - 从文件中读取数据,将其拆分为列表,然后获取该数据并将其放入函数中

python - 列表列表中不同元素的不同顺序排序

python - 求和python列表中的整数但不包括数字13和紧随其后的数字