1

Im stuck on a problem. Im trying to figure out how to write a program using PYTHON to check whether all the records are the same. so far I came up with the code below. How do I check all records and not just the data within the rows are equal?

import csv

index = 0

def all_same(items):
    return all(x == items[0] for x in items)


with open(r'C:\Users\Aaron\Desktop\testfolder\data.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter = ',')

    for row in readCSV:
            print (row)
            print (all_same(row))

the output looks like this:

['1', '1', '1', '2']
False
['1', '1', '1', '1']
True
['1', '1', '1', '1']
True
David Faber
  • 12,277
  • 2
  • 29
  • 40
gong15A
  • 77
  • 1
  • 2
  • 3

4 Answers4

2

You could try this:

import csv

with open(r'C:\Users\Aaron\Desktop\testfolder\data.csv') as csvfile:
  readCSV = csv.reader(csvfile, delimiter = ',')
  firstrow = readCSV.next()
  print(all(row==firstrow for row in readCSV))
Richard
  • 56,349
  • 34
  • 180
  • 251
1

If you wanted, you could put them all into a list and then check the value of that list:

data = []
for row in readCSV:
    data.append(row)

print (all_same(data))
David
  • 696
  • 6
  • 19
1

You may want to see a similar question on matching rows in Pandas dataframes listed below. One of the solutions in that question would apply here as well.

import pandas as pd

csv_filename = "file.csv" #your filepath
raw_data = pd.read_csv(csv_filename)

tmp = raw_data    
for idx, val in random_sample.iteritems():
    try:
        if np.isnan(val):
            continue
    except:
        pass
    tmp = tmp[tmp[idx] == val]
if len(tmp) == 1: print "match"

Efficiently find matching rows (based on content) in a pandas DataFrame

Community
  • 1
  • 1
unique_beast
  • 1,379
  • 2
  • 11
  • 23
1

Don't forget about Pandas

import pandas
df = pandas.read_csv(r'C:\Users\Aaron\Desktop\testfolder\data.csv')
pandas.unique(df.values)
bcollins
  • 3,379
  • 4
  • 19
  • 35