条件筛选多列python

时间:2019-09-29 04:12:00

标签: python pandas filter

我有5列,每列中的每一行都有0和1。我需要一次过滤所有带有“ 1”的内容。

我尝试过此操作,但导致错误:

df_2 = df_1[df_1.columns[0:5] == 1]
ValueError: Item wrong length 2 instead of 111249

1 个答案:

答案 0 :(得分:0)

我认为,如果要对已过滤列的每一行至少过滤一个any,则需要1

df_2 = df_1[(df_1.columns[0:5] == 1).any(axis=1)]

如果要过滤每列过滤的行中的所有all,请使用1

df_2 = df_1[(df_1.columns[0:5] == 1).all(axis=1)]