pandas:用列表值字典中的键和值替换列值



我有一个数据帧和一个字典,如下所示(但要大得多),

import pandas as pd
df = pd.DataFrame({'text': ['can you open the door?','shall you write the address?']})
dic = {'Should': ['can','could'], 'Could': ['shall'], 'Would': ['will']}

如果文本列中的单词可以在dic值列表中找到,我想替换它们,所以我做了以下操作,它适用于具有一个值但不适用于另一个值的列表,

for key, val in dic.items():
if df['text'].str.lower().str.split().map(lambda x: x[0]).str.contains('|'.join(val)).any():
df['text'] = df['text'].str.replace('|'.join(val), key, regex=False)
print(df)

我想要的输出是

text
0   Should you open the door?
1  Could you write the address?

最好的方法是更改逻辑并尽量减少panda步骤。

你可以制作一本字典,直接包含你理想的输出:

dic2 = {v:k for k,l in dic.items() for v in l}
# {'can': 'Should', 'could': 'Should', 'shall': 'Could', 'will': 'Would'}
# or if not yet formatted:
# dic2 = {v.lower():k.capitalize() for k,l in dic.items() for v in l}
import re
regex = '|'.join(map(re.escape, dic2))
df['text'] = df['text'].str.replace(f'b({regex})b',
lambda m: dic2.get(m.group()),
case=False, # only if case doesn't matter
regex=True)

输出(为清晰起见,作为文本2列):

text                         text2
0        can you open the door?     Should you open the door?
1  shall you write the address?  Could you write the address?

您可以在flatten dictionary中将小写字母用于键和值的d,然后将值替换为单词边界,最后使用Series.str.capitalize:

d = {x.lower(): k.lower() for k, v in dic.items() for x in v}

regex = '|'.join(r"b{}b".format(x) for x in d.keys())
df['text'] = (df['text'].str.lower()
.str.replace(regex, lambda x: d[x.group()], regex=True)
.str.capitalize())
print(df)
text
0     Should you open the door?
1  Could you write the address?

相关内容

  • 没有找到相关文章

最新更新