尝试基于两个互斥条件向df添加布尔列:
df['Type'] == 'CMBX'
df['SubType'].isin(['EM','NAHY'])
他们分开工作
df['Px Quoted'] = np.where(df['Type'] =='CMBX', True, False)
df[df['Type']=='CMBX'].head(5)
Out[72]:
Batch Type SubType Px Quoted
0 NaN CMBX True
1 NaN CMBX True
2 NaN CMBX True
3 NaN CMBX True
4 NaN CMBX True
或
df['Px Quoted'] = np.where(df['SubType'].isin(['EM','NAHY']), True, False)
df[df['SubType']=='EM'].head(5)
Out[74]:
Batch Type SubType Px Quoted
21 NY1530 CDX EM True
29 NY1530 CDX EM True
36 NY1530 CDX EM True
43 NY1530 CDX EM True
50 NY1530 CDX EM True
但以下内容不
df['Px Quoted'] = np.where(df['Type'] =='CMBX' or df['SubType'].isin(['EM','NAHY']), True, False)
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
不确定CMBX类型为何不能包含子类型['EM','NAHY']
中的任何内容,为什么会造成歧义?是因为其子类型为空?
答案 0 :(得分:1)
对于np.where
,您需要使用按位运算符:
df['Px Quoted'] = np.where((df['Type'] =='CMBX') | (df['SubType'].isin(['EM','NAHY'])), True, False)
在这里,我将or
更改为|