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
1
  • 1
    why your code are not comparing all your data? are u getting wrong output? Commented Feb 26, 2015 at 1:59

4 Answers 4

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))
Sign up to request clarification or add additional context in comments.

Comments

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))

Comments

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

1 Comment

thanks for that. I have figured another way of doing it. I might try your way as well.
1

Don't forget about Pandas

import pandas
df = pandas.read_csv(r'C:\Users\Aaron\Desktop\testfolder\data.csv')
pandas.unique(df.values)

1 Comment

thanks for that. Im checking all data are the same length not whether they are unique or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.