0

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'])
mountainclimber11
  • 1,339
  • 1
  • 28
  • 51
  • 1
    remove the `dfx[...]` on the right hand side of the equal sign: `dfx['short'] = np.where(dfx['value'].strip() == 'Annual','A',dfx['short'])` – pault Feb 22 '19 at 20:57
  • Possible duplicate of [Pandas: Ternary conditional operator for setting a value in a DataFrame](https://stackoverflow.com/questions/46525270/pandas-ternary-conditional-operator-for-setting-a-value-in-a-dataframe) – pault Feb 22 '19 at 20:59

1 Answers1

1

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

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