【发布时间】:2021-04-02 15:08:24
【问题描述】:
给定一个测试数据集如下:
city district ... Q3:*your age[open question] Q4:*skill[open question]
0 bj cy ... 45 R
1 bj cy ... 34 Python
我需要使用正则表达式重命名列,以提取* 和[ 之间的内容,如果存在your,还要删除它们。请注意,在实际情况下,我有很多问题列如下。
df.columns
Out[112]:
Index(['city', 'district', 'name', 'Q1:*your tel[open question]',
'Q2:*your profession[close question]', 'Q3:*your age[open question]',
'Q4:*skill[open question]'],
dtype='object')
预期的列将如下所示:
['city', 'district', 'name', 'tel', 'profession', 'age', 'skill']
如何在 Pandas 和正则表达式中做到这一点?非常感谢。
【问题讨论】:
标签: python-3.x regex pandas dataframe