1 回答

TA贡献1816条经验 获得超4个赞
国际自然分析联盟,
我们可以利用一些正则表达式和贪婪匹配,使用匹配定义模式之间的所有内容.*
import re
df.columns = [re.search(':(.*)-',i).group(1) for i in df.columns.str.strip()]
print(df.columns)
sales platforms Advertising letters
0 Not Selected None
编辑:
与贪婪的匹配,我们可以使用+?
+? Quantifier — Matches between one and unlimited times, as few times as possible, expanding as needed (lazy)
Q36r9: sales platforms - Before purchasing a new car Q40r1c3: WeChat - Looking for a new car - And now if you think again - Which social media platforms or sources would you use in each situation?
0 1
import re
[re.search(':(.+?)-',i).group(1).strip() for i in df.columns]
['sales platforms', 'WeChat']
添加回答
举报