regex - 根据名称列表更改 pandas 列中的文本
问题描述
背景
我有以下示例df
import pandas as pd
Names = [list(['Jon', 'Mith', 'jon', 'John']),
list(['Mark', 'Marky', 'marcs']),
list(['Bob', 'bobby', 'Bobs'])]
df = pd.DataFrame({'Text' : ['Jon J Mmith is Here and jon John from ',
'When he came Mark was Marky but not marcs so',
'I like Bob and bobby and also Bobs diner '],
'P_ID': [1,2,3],
'P_Name' : Names
})
#rearrange columns
df = df[['Text', 'P_ID', 'P_Name']]
df
Text P_ID P_Name
0 Jon J Mmith is Here and jon John from 1 [Jon, Smith, jon, John]
1 When he came Mark was Marky but not marcs 2 [Mark, Marky, marcs]
2 I like Bob and bobby and also Bobs diner 3 [Bob, bobby, Bobs]
这df
是此处看到的“老问题”的变体Alter text in pandas column based on names。我的新问题和“新问题”的唯一区别df
是列中名称的格式,P_Name
如下所示:
#old names from old question
array(['Mmith, Jon J', 'Hider, Mary', 'Doe, Jane Ann', 'Tucker, Tom'], dtype=object)
#new names from new question
array([list(['Jon', 'Smith', 'jon', 'John']),
list(['Mark', 'Marky', 'marcs']), list(['Bob', 'bobby', 'Bobs'])], dtype=object)
目标
在Text
列中,添加 **BLOCK**
与在中找到的值相对应的值(例如[Jon, Mmith, jon, John]
)P_Name
问题
当我使用“老问题”中的解决方案时,该解决方案取自根据名称更改 pandas 列中的文本
df['Text'].replace(df['P_Name'].str.split(', *').apply(lambda l: ' '.join(l[::-1])),'**BLOCK**',regex=True)
我收到以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-79-895f7ea46849> in <module>()
----> 1 df['Text'].replace(df['P_Name'].str.split(', *').apply(lambda l: ' '.join(l[::-1])),'**BLOCK**',regex=True)
/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
2353 else:
2354 values = self.asobject
-> 2355 mapped = lib.map_infer(values, f, convert=convert_dtype)
2356
2357 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer (pandas/_libs/lib.c:66645)()
<ipython-input-79-895f7ea46849> in <lambda>(l)
----> 1 df['Text'].replace(df['P_Name'].str.split(', *').apply(lambda l: ' '.join(l[::-1])),'**BLOCK**',regex=True)
TypeError: 'float' object is not subscriptable
期望的结果
我想要以下内容,类似于“老问题”中的答案Alter text in pandas column based on names
Text P_ID P_Name
0 **BLOCK** J **BLOCK** is Here and **BLOCK** **BLOCK** from 1 [Jon, Smith, jon, John]
1 When he came **BLOCK** was **BLOCK** but not **BLOCK** 2 [Mark, Marky, marcs]
2 I like **BLOCK** and **BLOCK** and also **BLOCK** diner 3 [Bob, bobby, Bobs]
问题
鉴于我的P_Name
列现在包含列表列表,我如何实现我想要的结果?
解决方案
IIUC,您需要series.replace
将列表作为参数:
to_replace :str、regex、list、dict、Series、int、float 或 None
df=df.assign(Text=df.Text.replace(df.P_Name,'**BLOCK**',regex=True))
推荐阅读
- android - Android 目标 SDK 从 26 移动到 28,错误:指定的子已经有父。您必须先在孩子的父母上调用 removeView()
- javascript - 在日视图中呈现角度 ui 日历上的数据
- python - 如何在一组图像上测试 Keras 模型?
- c# - 无法获取给定字符串中的字母/数字/特殊字符的数量
- python - 使用 Python 在一个图中绘制一系列股票数据
- azure - Microsoftapplicationinsight.dll 以这样的方式进行编辑,以便使用它离线将数据发布到云端
- c# - 循环遍历各种枚举的函数
- macos - 以编程方式禁用 NSWindow 捕捉/粘性
- python-3.x - 将 npz 转换为 csv 格式时出现关键错误
- weblogic - 带有 TwelveMonkeys 的 ClassCastExceptions 甚至在 Weblogic 12c 上的隔离应用程序类加载器上