简体   繁体   中英

Regex PANDAS dataframe

I have a dataframe that looks something like:

import pandas as pd

df= pd.DataFrame({'type':['-','L1iability','-','Liability1','-'],'Amount':[10,-10,20,-20,5]})
df

I want to replace everything other than "-" in the type column with "3rd". How do I do this with the replace command and regex ?

IIUC mask

df['3rdcol']=df.type.mask(~(df.type=='-'),'Liability1')
df
Out[303]: 
   Amount        type      3rdcol
0      10           -           -
1     -10  L1iability  Liability1
2      20           -           -
3     -20  Liability1  Liability1
4       5           -           -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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