[英]How to find the index of the not-null item which not in a list?
我的系统是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
我想在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()
采用:
getindex=df.index[(~df['col2'].isin(['a','b','c']) & df['col2'].notna())]
print (getindex)
Int64Index([0], dtype='int64')
如果想要选择第一个值而没有错误,如果值不存在:
print (next(iter(getindex), 'no match'))
0
如果想要if empty
语句使用Index.empty
进行测试:
if not getindex.empty:
print (getindex)
如果从列表中选择第一个数组添加[0]
,您的解决方案应该有效:
getindex=np.where(~df['col2'].isin(['a','b','c']) & df['col2'].notna())[0]
print (getindex)
[0]
如果条件,请修改你
getindex=np.where(~df['col2'].isin(['a','b','c']) & df['col2'].notna())
if any(~df['col2'].isin(['a','b','c']) & df['col2'].notna()): # change here to any
print(getindex)
(array([0], dtype=int64),)
同样基于你的单词#if getindex is not empty, print it
if len(getindex)!=0:
print(getindex)
(array([0], dtype=int64),)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.