mountainclimber11
mountainclimber11

Reputation: 1400

numpy where: add value where

I have this data frame:

           ticker                   value short
0  APPL US Equity    Quarterly and Annual      
1  TTMT IN Equity    Quarterly and Annual      
2   BUD US Equity  Semi-Annual and Annual      
3    PS US Equity                  Annual      

I want this:

           ticker                   value short
0  APPL US Equity    Quarterly and Annual      
1  TTMT IN Equity    Quarterly and Annual      
2   BUD US Equity  Semi-Annual and Annual      
3    PS US Equity                  Annual  A    

here is my attempt:

dfx['short'] = dfx[np.where(dfx['value'].strip() == 'Annual','A'
                                    ,dfx['short'])]

this works:

dfx['short'] = np.where(dfx['value'] == 'Annual','A',dfx['short'])

Upvotes: 0

Views: 507

Answers (1)

Heikki Pulkkinen
Heikki Pulkkinen

Reputation: 277

You can set a only part of the values in a column with .loc:

mask = dfx['value'].strip() == 'Annual'
dfx.loc[mask, 'short'] = 'A'

Upvotes: 1

Related Questions