Моя система - python3.6, с numpy 1.16.2, scipy 1.2.1, matplotlib 3.0.3
import pandas as pd
import numpy
df=pd.DataFrame({'col1':['a','b','c'],'col2':['d',numpy.NaN,'c'],'col3':['c','b','b']})
df = df.astype({"col2": 'category'})
print(df)
. Вывод вышеприведенного сценария:
col1 col2 col3
0 a d c
1 b NaN b
2 c c b
Iхотите найти индекс ненулевого элемента в серии col2
, категория которого не входит в ['a','b','c']
В этом случае d
не является null
и не находится в ['a','b','c']
, тогдаожидаемый результат должен быть индексом d
, который равен 0
Мое решение как удар:
getindex=numpy.where(~df['col2'].isin(['a','b','c']) & df['col2'].notna())
#if getindex is not empty, print it
if not all(getindex):
print(getindex)
Вывод моего сценария решения:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()