这是检查所有值是否为零的方法:很简单
并且不需要高级功能如上述答案。只有基本的
过滤、if 循环和变量分配等功能。
首先是检查一列是否只有零的方法,并且
第二个是如何查找所有列是否都为零。它
打印和回答声明。
检查一列是否只有零值的方法:
先做一个系列:
has_zero = df[4] == 0
# has_zero is a series which contains bool values for each row eg. True, False.
# if there is a zero in a row it result will be "row_number : True"
下一个:
rows_which_have_zero = df[has_zero]
# stores the rows which have zero as a data frame
下一个:
if len[rows_which_have_zero] == total_number_rows:
print("contains only zeros")
else:
print("contains other numbers than zero")
# substitute total_number_rows for 3220
上述方法只检查 rows_which_have_zero 是否等于列中的行数。
查看所有列是否只有零的方法:
它使用上述函数并将其放入 if 循环中。
no_of_columns = 1559
value_1 = 1
if value_1 <= 1559
has_zero = df[value_1] == 0
rows_which_have_zero = df[has_zero]
value_1 += 1
if len[rows_which_have_zero] == 1559
no_of_rows_with_only_zero += 1
else:
return
检查所有行是否只有零:
#since it doesn't matter if first 3 columns have zero or not:
no_of_rows_with_only_zero = no_of_rows_with_only_zero - 3
if no_of_rows_with_only_zero == 1559:
print("there are only zero values")
else:
print("there are numbers which are not zero")
上面检查 no_of_rows_with_only_zero 是否等于行数(即 1559 减去 3,因为只需要检查第 4 - 1559 行)
更新:
# convert the value_1 to str if the column title is a str instead of int
# when updating value_1 by adding: convert it back to int and then back to str