我有这个数据框:
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'])
答案 0 :(得分:1)
您可以使用.loc设置列中值的仅一部分:
mask = dfx['value'].strip() == 'Annual'
dfx.loc[mask, 'short'] = 'A'