1. 原始数据
想把Sex一列,根据下划线且分为两列数据
In [193]: user_info
Out[193]:
age city sex birth
name
tom 18.0 Bei Jing a_1 2000-02-10
bob 30.0 Shang Hai a_2 1988-10-17
mary NaN Guang Zhou a_3 NaT
james 40.0 Shen Zhen b_1 1978-08-08
andy NaN NaN b_2 NaT
alice 30.0 b_3 1988-10-17
2. 性别一类的数据
In [190]: user_info.sex
Out[190]:
name
tom a_1
bob a_2
mary a_3
james b_1
andy b_2
alice b_3
Name: sex, dtype: object
3. 使用extract
和正则表达式
进行提取
In [191]: user_info.sex.str.extract("(\w)_(\w)")
Out[191]:
0 1
name
tom a 1
bob a 2
mary a 3
james b 1
andy b 2
alice b 3