简体   繁体   中英

Create new column on basis of another column in Pandas

I have a column in Dataframe by the name STATUS, which contains value "AMEND ORDER" or it is empty. I need to create a FLAG on the basis of that column, if it values contains AMEND word in the value, set FLAG 1, or 0. I am trying to use the below code but it errors it.

t[t['STATUS'].str.contains('AMEND')] 

If empty are missing values or None s chain another mask by |for bitwise OR and cast to integers for True -> 1, False - > 0 mapping:

t['FLAG'] = (t['STATUS'].str.contains('AMEND') | t['STATUS'].isna()).astype(int)

If empty is empty string compare values by Series.eq :

t['FLAG'] = (t['STATUS'].str.contains('AMEND') | t['STATUS'].eq('')).astype(int)

This could be one way to do it

mydat.loc[mydat['Status']=="",'flag']=0
mydat.loc[mydat['Status']=="AMEND ORDER",'flag']=1

Status flag

0 AMEND ORDER 1.0

1 AMEND ORDER 1.0

  1. "............." 0.0

3 AMEND ORDER 1.0

4 ".............." 0.0

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