繁体   English   中英

从字符串 pandas 中提取多个模式

[英]Extract multiple pattern from string pandas

使用从数据集中的列中提取的中间和最后一个字符串创建新列。

数据

Status             ID
Ok                 hello_dd           
Ok                 hello_aa_now       
No                 standard_cc        
no                 standard_ee_not  

想要的

Status             ID                        type
Ok                 hello_dd                  dd     
Ok                 hello_aa_now              aa
No                 standard_cc               cc
no                 standard_ee_not           ee

正在做

我能够提取最后一个字符串,但是,仍在研究如何提取中间字符串。

df['type'] = df['ID'].str.strip('_').str[-1]

任何建议表示赞赏。

假设您想在第一个_之后extract字符串:

df['type'] = df['ID'].str.extract(r'_([^_]+)')

split

df['type'] = df['ID'].str.split('_').str[1]

output:

  Status               ID type
0     Ok         hello_dd   dd
1     Ok     hello_aa_now   aa
2     No      standard_cc   cc
3     no  standard_ee_not   ee

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM