如何比较一个字符串的两列,并将一列中的字符串大小写替换为另一列中的字符串大小写?

2024-04-25 03:33:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两列句子和更新。我想将Url末尾Updates列中的每个单词与相应的句子word case匹配,并将其替换为句子中单词的case。你知道吗

我不知道该怎么做比较,谢谢你的帮助。 实际数据中有43k行具有不同的Url

示例代码:

import pandas as pd

dict1 = {'Updates': ['The new abc.com/Line','Its a abc.com/bright and abc.com/Sunny Day','abc.com/smartphone have taken our the abc.com/WORLD','abc.com/GLOBAL Warming is abc.com/Reaching its abc.com/peak'],
     'Sentences': ['The new line','Its a bright and sunny day','Smartphone have taken our the World','GLOBAL Warming is reaching its Peak ']
        }

df = pd.DataFrame(dict1)

当前O/p:

Sentences           Updates
The new line            The new abc.com/Line

Its a bright and sunny day          Its a abc.com/bright and abc.com/Sunny Day

Smartphone have taken our the World         abc.com/smartphone have taken our the abc.com/WORLD

GLOBAL Warming is reaching its Peak             abc.com/GLOBAL Warming is abc.com/Reaching its abc.com/peak
Expected O/P:

Sentences           Updates
The new line            The new abc.com/line

Its a bright and sunny day          Its a abc.com/bright and abc.com/sunny day

Smartphone have taken our the World         abc.com/Smartphone have taken our the abc.com/World

GLOBAL Warming is reaching its Peak             abc.com/GLOBAL Warming is abc.com/reaching its abc.com/Peak

Tags: andthecomnewishaveourglobal
1条回答
网友
1楼 · 发布于 2024-04-25 03:33:24

使用re

代码:

import re

dict1 = {
    'Sentences': [
        'The new line',
        'Its a bright and sunny day',
        'Smartphone have taken our the World',
        'GLOBAL Warming is reaching its Peak '
    ],
    'Updates': [
        'The new abc.com/Line',
        'Its a abc.com/bright and abc.com/Sunny Day',
        'abc.com/smartphone have taken our the abc.com/WORLD',
        'abc.com/GLOBAL Warming is abc.com/Reaching its abc.com/peak'
    ]
 }
for sentence, update in zip(dict1['Sentences'], dict1['Updates']):
    urls = [x.split("/")[-1] for x in update.split() if "/" in x]
    for url in urls:
        update = (re.sub(url, re.search(url, sentence, re.IGNORECASE).group(), update, flags=re.IGNORECASE))

    print(f"{sentence}\t{update}")

输出:

The new line    The new abc.com/line
Its a bright and sunny day  Its a abc.com/bright and abc.com/sunny Day
Smartphone have taken our the World abc.com/Smartphone have taken our the abc.com/World
GLOBAL Warming is reaching its Peak     abc.com/GLOBAL Warming is abc.com/reaching its abc.com/Peak

相关问题 更多 >