【发布时间】:2020-06-03 10:24:05
【问题描述】:
我有一个带有标题列的数据框(参见下面的示例)
import numpy as np
Fairytales_in = {'Titles': ['Fairy Tales',
'Tales.3.2.Dancing Shoes, ballgowns and frogs',
'Tales.2.4.6.Red Riding Hood',
'Fairies.1Your own Fairy godmother',
'Ogres-1.1.The wondrous world of Shrek',
'Witches-1-4Maleficient and the malicious curse',
'Tales.2.1.The big bad wolf',
'Tales.2.Little Red riding Hood',
'Tales.2.4.6.1.Why the huntsman is underrated',
'Tales.5.f.Cinderella and the pumpkin carriage',
'Ogres-1.Best Ogre in town',
'No.3.Great Expectations']}
Fairytales_in = pd.DataFrame.from_dict(Fairytales_in)
我想创建一个新列,其中包含与标题列完全相同的字符串,但仅当它是副标题时。 (例如 Tales.3.2. 或 Ogres-1.1. 或 Witches-1-4 或 Tales.5.f)。
This would be my expected output:
Fairytales_expected_output = {'Titles': ['Fairy Tales',
'Tales.3.2.Dancing Shoes, ballgowns and frogs',
'Tales.2.4.6.Red Riding Hood',
'Fairies.1Your own Fairy godmother',
'Ogres-1.1.The wondrous world of Shrek',
'Witches-1-4Maleficient and the malicious curse',
'Tales.2.1.The big bad wolf',
'Tales.2.Little Red riding Hood',
'Tales.2.4.6.1.Why the huntsman is underrated',
'Tales.5.f.Cinderella and the pumpkin carriage',
'Ogres-1.Best Ogre in town',
'No.3.Great Expectations'],
'Subheading': ['NaN',
'Tales.3.2.Dancing Shoes, ballgowns and frogs',
'NaN',
'NaN',
'Ogres-1.1.The wondrous world of Shrek',
'Witches-1-4Maleficient and the malicious curse',
'Tales.2.1.The big bad wolf',
'NaN',
'NaN',
'Tales.5.f.Cinderella and the pumpkin carriage',
'NaN',
'NaN']}
Fairytales_expected_output = pd.DataFrame.from_dict(Fairytales_expected_output)
我一直在努力寻找一种方法让我的模式只匹配副标题。无论我尝试什么,仍然包含第一级或第三级标题。 This question 的问题或多或少相同,但它是在 C# 中的,我无法让它在我的用例中工作。
这是我迄今为止尝试过的:
Fairytales_in['Subheading'] = Fairytales_in.Titles.str.extract(r'(^(?:\w+\.|\-\d{1}\.\d{1}\.)\W*(?:\w+\b\W*){1,100})$')
但正如您所见,它不会产生预期的结果。我一直在尝试使用 regex101.com,但我已经坚持了两天了。任何有关修复我的模式的帮助将不胜感激!
【问题讨论】: