Проблема с:
m=np.where((df1['scorehigh?']=='yes')) & (df1['matches'] != '[0]')
Если параметр после выхода маски не является массивом позиций совпадающих значений.
df1['matches'].fillna('[0]',inplace = True)
df1['scorehigh?'] = df1['league'].apply(lambda a: 'yes' if a == 'Active' or a == 'Super Active' else 'no')
df1['greaterthan10?'] = (['yes' if any(int(a)>10 for a in i) else 'no'
for i in df1['matches'].str.findall('\d+')])
Используйте вложенные numpy.where
с указанием None
, если совпадений нет, также для второй маски использовалась только df1['matches'] != '[0]'
:
df1['Finals?'] = np.where((df1['scorehigh?']=='yes')&(df1['greaterthan10?'] == 'yes'), 'YES',
np.where(df1['matches'] != '[0]', 'NO', None))
Или numpy.select
:
df1['Finals?'] = np.select([(df1['scorehigh?']=='yes')& (df1['greaterthan10?'] == 'yes'),
df1['matches'] != '[0]'], ['YES', 'NO'], default=None)
print (df1)
league matches scorehigh? greaterthan10? Finals?
0 Active [[1, 0, 50,], [2, 0, 14,]] yes yes YES
1 Active [[1, 0, 0,], [2, 0, 4,]] yes no NO
2 Active [[1, 0, 50,], [2, 0, 14,]] yes yes YES
3 Super Active [[1, 0, 50,], [2, 0, 14,]] yes yes YES
4 Low [[1, 0, 50,], [2, 0, 14,]] no yes NO
5 Low [[1, 0, 5,], [2, 0, 5,]] no no NO
6 Low [[1, 0, 40,], [2, 0, 10,]] no yes NO
7 Super Active [0] yes no None
8 Super Active [0] yes no None
9 Super Active [0] yes no None
10 Super [0] no no None
11 Low [0] no no None
a=df1['Finals?'].value_counts()
print(a)
NO 4
YES 3
Name: Finals?, dtype: int64
Если используются оба вывода условий, разные:
df1['Finals?'] = np.select([(df1['scorehigh?']=='yes')& (df1['greaterthan10?'] == 'yes'),
(df1['scorehigh?']=='yes') & (df1['matches'] != '[0]')],
['YES', 'NO'], default=None)
print (df1)
league matches scorehigh? greaterthan10? Finals?
0 Active [[1, 0, 50,], [2, 0, 14,]] yes yes YES
1 Active [[1, 0, 0,], [2, 0, 4,]] yes no NO
2 Active [[1, 0, 50,], [2, 0, 14,]] yes yes YES
3 Super Active [[1, 0, 50,], [2, 0, 14,]] yes yes YES
4 Low [[1, 0, 50,], [2, 0, 14,]] no yes None
5 Low [[1, 0, 5,], [2, 0, 5,]] no no None
6 Low [[1, 0, 40,], [2, 0, 10,]] no yes None
7 Super Active [0] yes no None
8 Super Active [0] yes no None
9 Super Active [0] yes no None
10 Super [0] no no None
11 Low [0] no no None
a=df1['Finals?'].value_counts()
print(a)
YES 3
NO 1
Name: Finals?, dtype: int64