我有一个这样的数据框,
ColA Result_ColA ColB Result_ColB Result_ColC
1 True 1 True True
2 False 2 True False
3 True 3 True False
我想在python列表中标识行号,在任何Result_列中都存在False值。
对于给定的数据帧,错误列表中将包含行号[2和3]。考虑从1开始的行号。
类型错误回溯:
ReqRows = np.arange(1, len(Out_df)+ 1)[Out_df.eq(False).any(axis=1).values].tolist()
Traceback (most recent call last):
File "<ipython-input-92-497c7b225e2a>", line 1, in <module>
ReqRows = np.arange(1, len(Out_df)+ 1)[Out_df.eq(False).any(axis=1).values].tolist()
File "C:\Users\aaa\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\ops.py", line 1279, in f
return self._combine_const(other, na_op)
File "C:\Users\aaa\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\frame.py", line 3625, in _combine_const
raise_on_error=raise_on_error)
File "C:\Users\aaa\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3162, in eval
return self.apply('eval', **kwargs)
File "C:\Users\aaa\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3056, in apply
applied = getattr(b, f)(**kwargs)
File "C:\Users\aaa\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\internals.py", line 1115, in eval
transf(values), other)
File "C:\Users\aaa\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\internals.py", line 2247, in _try_coerce_args
raise TypeError
TypeError
答案 0 :(得分:2)
根据DataFrame的长度(从1
开始创建辅助1d数组,并对其进行过滤,以筛选出所有包含至少一个False
的行:
a = np.arange(1, len(df) + 1)[df.eq(False).any(axis=1).values].tolist()
print (a)
[2, 3]
您还可以先在DataFrame.select_dtypes
之前仅获取布尔列:
arr = (df.select_dtypes(include=[bool])).eq(False).any(axis=1).values
a = np.arange(1, len(df) + 1)[arr].tolist()
答案 1 :(得分:2)
也:
list(df.loc[(df==False).any(axis=1)].index+1)
#[2, 3]