简体   繁体   English

如何从 python 中的 csv 文件中删除包含全零的列?

[英]How to delete columns containing all zeros from a csv file in python?

I want to delete the columns from a csv file that contain all zeros for example like the column f, g, h, k, l.我想从包含全零的 csv 文件中删除列,例如列 f、g、h、k、l。 The csv file in question is populated with the script so it is not possible to hardcode the columns.有问题的 csv 文件填充了脚本,因此无法对列进行硬编码。 I would be really grateful if you could help with it.如果您能提供帮助,我将不胜感激。

File.csv
a,b,c,d,e,f,g,h,i,j,k,l
1,5,4,4,5,0,0,0,6,3,0,0
2,5,3,4,1,0,0,0,7,1,0,0
1,2,6,4,1,0,0,0,9,2,0,0
5,7,3,4,2,0,0,0,2,2,0,0
7,2,9,4,3,0,0,0,1,1,0,0

Resultant expected预期结果

File.csv
a,b,c,d,e,i,j
1,5,4,4,5,6,3
2,5,3,4,1,7,1
1,2,6,4,1,9,2
5,7,3,4,2,2,2
7,2,9,4,3,1,1

The following approach could be used with the csv library:以下方法可用于csv库:

  1. Read the header in阅读 header 中的
  2. Read the rows in读取中的行
  3. Transpose the list of rows into a list of columns (using zip )将行列表转置为列列表(使用zip
  4. Use a set to drop all columns that only contain 0使用集合删除所有仅包含0的列
  5. Write out the new header写出新的 header
  6. Write out the transposed list of columns as a list of rows.将转置的列列表写为行列表。

For example:例如:

import csv
    
with open('file.csv', newline='') as f_input:
    csv_input = csv.reader(f_input)
    header = next(csv_input)   # read header
    columns = zip(*list(csv_input))   # read rows and transpose to columns
    data = [(h, c) for h, c in zip(header, columns) if set(c) != set('0')]
    
with open('file2.csv', 'w', newline='') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(h for h, c in data)   # write the new header
    csv_output.writerows(zip(*[c for h, c in data]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM