I have a big csv file that I want to edit. The editing here means deleting the columns that have only one value. So far, I wrote this (since I'm a newbie in Python I'm stuck and not sure if that is the right solution for the problem):
import csv
import collections
import numpy as np
number_of_rows = 2432
interseting_cols = []
col_values = collections.defaultdict(list)
col_values_named = collections.defaultdict(list)
new_row = collections.defaultdict(list)
inputFile = open('input.csv', 'r',newline='');
outputFile= open('output.csv','w')
reader = csv.reader(inputFile)
writer = csv.writer(outputFile)
#skip field names
next(reader)
for row in reader:
for col, value in enumerate(row):
col_values[col].append(value)
#each column is now saved col_values ( without the headers )
for i in range(len(col_values)):
if len(set(col_values[i][:(number_of_rows-1)])) != 1:
interseting_cols.append(i)# saved the index of the columns with valid values
inputFile.seek(0)
# reading the file again now with headers
for row in reader:
for col, value in enumerate(row):
col_values_named[col].append(value)# saving the columns now with header
# generating a new CSV file, only with interessting columns :
for i in range(number_of_rows):
print("i value ",i)
for j in range(len(interseting_cols)): # I'm not sure about this PART !!!!
new_row.append(col_values_named[interseting_cols[j]])
writer.writerow(new_row)
Any idea how to do the last loop? Or is there is a better way to solve this?
UPDATE say the file looks like
---------------------------------------------------
|A|B |C |D |F |G|H |I|J |K |
---------------------------------------------------
1 |1|NULL|444 |201|0.01|A|NULL|4|9.5|NULL|
---------------------------------------------------
2 |2|NULL|NULL|201|0 |A|NULL|4|9.5|NULL|
---------------------------------------------------
3 |4|NULL|444 |201|0 |A|NULL|4|9.5|NULL|
---------------------------------------------------
4 |1|NULL|444 |201|0 |A|NULL|4|9.5|NULL|
in this case the result should only include only three columns A,C and F