Jennifer Therese
Jennifer Therese

Reputation: 1295

How to add suffix to a column based on a value condition in pandas?

I have a df

 Sent   Words     Tags    POS
Sent1   Realt       O     NNP
Sent1   Need        O     NNP
Sent1   Guys        O     NNP
Sent2   20,000    Limit   PRP
Sent3    EUR     Currency NNP

Where ever the tag is 'O', it needs to be combined with its corresponding POS value. Ex- O-NNP For other tags than O, leave the value as it is.

Desired output:

Sent    Words       Tags      POS
Sent1   Realt       O-NNP     NNP
Sent1   Need        O-NNP     NNP
Sent1   Guys        O-NNP     NNP
Sent2   20,000      Limit     PRP
Sent3    EUR        Currency   NNP

Kindly help.

Upvotes: 0

Views: 802

Answers (1)

jezrael
jezrael

Reputation: 863751

Use Series.mask by compare for equal:

df['Tags'] = df['Tags'].mask(df['Tags'].eq('O'), 'O_' + df['POS'])

Or use Series.str.cat:

df.loc[df['Tags'].eq('O'), 'Tags'] = df['Tags'].str.cat(df['POS'], sep='_')

Working same like:

df.loc[df['Tags'].eq('O'), 'Tags'] = 'O_' + df['POS']

Or:

df.loc[df['Tags'].eq('O'), 'Tags'] += '_' + df['POS']

print (df)
    Sent   Words      Tags  POS
0  Sent1   Realt     O_NNP  NNP
1  Sent1    Need     O_NNP  NNP
2  Sent1    Guys     O_NNP  NNP
3  Sent2  20,000     Limit  PRP
4  Sent3     EUR  Currency  NNP

Upvotes: 3

Related Questions