Skip to content Skip to sidebar Skip to footer

To Extract Specific Columns From A Csv File And Copy It To Another Using Python

I have a CSV file which is actually a matrix of 0's and 1's. I need to exclude those columns that have 0's and pick only those that have 1's and copy them to another CSV file. Her

Solution 1:

If you need to exclude all columns that have any zeroes in them, then first you need to read the whole file in memory -- because only after having seen every row will you know which columns have any zeroes! This is a logical need -- whatever language you use the need will remain, it's intrinsic to the problem

So, for example:

allrows = list(reader)

Now, allrows is a list of dictionaries, whose items are strings, presumably 0 or 1. Now, you could do:

keepcols =[cforcin allrows[0]ifall(r[c]!='0'for r in allrows)]

...not the fastest approach, but hopefully very, very simple to understand!

Once you do know which columns you want to keep, prepare a DictWriter instance w with those columns as the headers and the extrasaction='ignore' argument (so it will ignore "extra" keys in the dicts passed to it, and finally

w.writerows(allrows)

If you mean something different than "exclude all columns which have any zeroes in them", then please clarify exactly what you do mean by "i need exclude those columns that have 0's" because I can't interpret it differently.

Solution 2:

reader = csv.DictReader(open("test1.csv", "r"), [])

fordatain reader:
    ifdata[column header] != 0:
         print data[column header]

Post a Comment for "To Extract Specific Columns From A Csv File And Copy It To Another Using Python"