Home > database >  Unique elements of all the columns of CSV file in Python without using Pandas
Unique elements of all the columns of CSV file in Python without using Pandas

Time:04-25

I am trying to get the unique values of all the columns in the CSV. I am getting the column number and creating sets for all the columns and trying to go through the csv data and find the unique columns. But the second loop executes only once.

decoded_file = data_file.read().decode('utf-8')
reader = csv.reader(decoded_file.splitlines(),
                            delimiter=',')
list_reader = list(reader)
data = iter(list_reader)
next(data) #skipping the header
col_number = len(next(data))
col_sets = [set() for i in range(col_number)]

for col in range(col_number):
   for new_row in data:
       col_sets[col].add(new_row[col])
   print(col_sets[col])

I need to get all the unique values for each column and add it to col_sets to access it. What is the best way to do this?

CodePudding user response:

Everything is good, but you should just change the order of iterations.


for new_row in data:
    for col in range(col_number):
        col_sets[col].add(new_row[col])
print(col_sets)
  • Related