早上好,
我有一个仅具有True和False值的数据框,并且想要获取值True退出的行索引。我尝试了这个:
[i for i in df_str[df_str.columns.values] if i== True]
但这将返回一个空数组。我怎样才能做到这一点?
这是一种方法。为了演示,我使用合成数据。
df = pd.DataFrame({"a": np.random.choice([True, False], 10),
"b": np.random.choice([True, False], 10)})
print(df)
# a b
# 0 False True
# 1 True False
# 2 False True
# 3 True True
# 4 False False
# 5 True False
# 6 False True
# 7 True False
# 8 True False
# 9 True True
# 'a' and 'b' are the columns you'd like to search
df[df[["a", "b"]].sum(axis=1) > 0].index.to_list()
# ==> [0, 1, 2, 3, 5, 6, 7, 8, 9]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句