【问题标题】:Extracting the text starting with a character and ends with another into new column in python将以一个字符开头并以另一个字符结尾的文本提取到python中的新列中
【发布时间】:2021-10-24 11:36:24
【问题描述】:

我正在尝试从列中给出的链接中提取 ID。

ID 在“tt”之后开始,在“/”之前结束。正在尝试将其提取到新列中。

输入数据集:

输出数据集:

【问题讨论】:

  • ID 列是否应该为空?
  • 没有。应该填114709。编辑更新了。

标签: python pandas find extract extend


【解决方案1】:

如果要修改链接,请使用带有str.replace的正则表达式:

df['Link'] = df['Link'].str.replace(r'(.*/title/tt)(\d+)(/.*)', r'\1\2\3/ \2')

如果我相信您的示例不正确,并且您实际上希望创建一个带有数字的新列:

df['ID'] = df['Link'].str.extract(r'(?:.*/title/tt)(?P<ID>\d+)(?:/.*)')

输出:

     Movie                                                   Link       ID
0  movie 1  http://www.imdb.com/title/tt0114709/?ref_=fn_tt_tt_1|  0114709

【讨论】:

  • 我正在寻找您提到的第二个代码。有效。谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-05-08
  • 2023-03-23
  • 2016-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多