簡體   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