python - 如何从数据框中的列中提取强标签并附加或替换该单元格?

标签 python pandas

我有一个数据框,其中的列包含我需要提取的粗体字母。有53000行27列,有粗体字。

array(['Candidate initial submission',
      'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>Candidate initial submission',
      'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Interview 1</strong> and <strong>Scheduled</strong> with Stage Date 02 August, 2018, 12:00 am IST - UTC +05:30'],
     dtype=object)

最佳答案

使用pandas.Series.str.extractall :

import pandas as pd

lst = ['Candidate initial submission',
 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>Candidate initial submission',
 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Interview 1</strong> and <strong>Scheduled</strong> with Stage Date 02 August, 2018, 12:00 am IST - UTC +05:30']


df = pd.DataFrame(data=lst, columns=['text'])

result = df.text.str.extractall('<strong>(.+?)</strong>')

输出

                         0
  match                   
1 0           CV Submitted
  1       Feedback Pending
  2       Client CV Review
  3      Feedback Awaiting
2 0           CV Submitted
  1       Feedback Pending
  2            Interview 1
  3              Scheduled

正则表达式模式'<strong>(.+?)</strong>'将匹配 <strong> 之间的所有内容和</strong> ,文字尽可能少。要了解有关正则表达式的更多信息,请参阅here .

关于python - 如何从数据框中的列中提取强标签并附加或替换该单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58640352/

相关文章:

python - 从 Python 传递 shellcode 作为命令行参数

python - 在 Python 中自省(introspection)构造函数 __init__ 的参数

python - m 上三角矩阵中的最小值,其索引为元组列表

python - Pandas :找到部分字符串并在新列中使用它

python - Pandas + Matplotlib,让条形图中的一种颜色脱颖而出

c# - 定义 Python 类

python - 在 Python 中将 STDOUT 十六进制输出转换为字符串

python - 如何使用 Moviepy 和 Pygame 播放 mp4 电影

python - 带有 NaN 掩码的数据帧的加权平均值

python - 根据列中的值删除行中的值,然后在 Pandas 中将单元格拆分为多行