简体   繁体   English

正则表达式PANDAS数据框

[英]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". 我想用“ 3rd”替换类型列中除“-”以外的所有内容。 How do I do this with the replace command and regex ? 我该如何使用replace命令和regex?

IIUC mask 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           -           -

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

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