numpy其中:在其中添加值

时间:2019-02-22 20:56:39

标签: python python-3.x

我有这个数据框:

           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      

我想要这个:

           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    

这是我的尝试:

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

这有效:

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

1 个答案:

答案 0 :(得分:1)

您可以使用.loc设置列中值的仅一部分:

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