如何删除具有所有零值但不包含非零值的零的行

时间:2018-11-14 01:27:40

标签: python pandas

我有一个具有以下值的csv文件:

0,0,0,0,October 29 2018 16:35:04
0,1,2,0,October 30 2018 11:40:04
0,0,0,0,November 25 2018 04:20:13

我想删除前4列中值为零的行:

0,0,0,0,October 29 2018 16:35:04 #remove this
0,1,2,0,October 30 2018 11:40:04 #this should stay
0,0,0,0,November 25 2018 04:20:13 #remove this

2 个答案:

答案 0 :(得分:2)

IIUC使用df[...]

print(df[~(df[df.columns[:4]]==0).all(1)])

稍微好一点(感谢@jpp),请使用iloc

print((df.iloc[:, :4] == 0).all(1))

两个输出:

   0  1  2  3                         4
1  0  1  2  0  October 30 2018 11:40:04

输出列可能不正确,因为我不知道实际的列。

答案 1 :(得分:1)

有很多方法可以完成您要问的事情,但是您有两项任务:

  • 阅读.csv,您可以使用csv.reader
  • 遍历所有内容,您可以使用简单的for循环
  • 检查一些条件,您需要检查整数值是否为0,int(row[col]) == 0
  • 将符合条件的行写到新的.csv中,您可以使用csv.writer完成

这是一个工作脚本,不需要标准csv即可使用这些库,而无需外部库:

from csv import reader, writer

with open('input.csv', 'r') as input_file:
    with open('output.csv', 'w', newline='') as output_file:
        csv_in = reader(input_file)
        csv_out = writer(output_file)
        for row in csv_in:
            if not all([int(row[col]) == 0 for col in range(0, 4)]):
                csv_out.writerow(row)
相关问题