Create new column on basis of another column in Pandas

Sunil Raperia :

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')] 
jezrael :

If empty are missing values or Nones 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)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=395064&siteId=1