Comparing two rows from a csv file in Python

python csv compare rows
compare two large csv files for differences python
compare two rows in python
compare two columns of different files and print if it matches python
python compare column values
compare two csv files python
how to compare two csv files in python using pandas
how to map two csv files in python

I am trying to write a manhattan_distance function which would compare two rows from a csv file. I already have a function which reads all the rows from a file but I am unable to access 'row' from this function to use for comparison in the second function. The error I get is "name 'row' not defined". Here is my code:

def load_from_csv(filename):    
    with open(filename,'r') as csvfile: 
        reader = csv.reader(csvfile, delimiter=',', quotechar='|') 
        for row in reader:
            print(row)
        return row 

load_from_csv('data.csv') 

def manhattan_distance(x,y):
 return sum(abs(a-b) for a,b in zip(x,y))

print (manhattan_distance(row[1],row[2]))

a sample from the csv file (first two rows):

14.23 1.71 2.43 15.6 127 2.8 3.06 0.28 2.29 5.64 1.04 3.92 1065

13.2 1.78 2.14 11.2 100 2.65 2.76 0.26 1.28 4.38 1.05 3.4 1050

Sorry if this is a silly question but I'm not good with Python and I am struggling to find a relevant answer online.

You are trying to return a variable that does not exist; row

for row in reader:
        print(row)
return row

You declare for row in reader which means you can use row within the loop. However, your return statement falls outside of the loop, therefore causing the error that it is not defined.

Try calling your CSV rows to a list and returning the list from the function.

def load_from_csv(filename):    
    with open(filename,'r') as csvfile: 
        reader = csv.reader(csvfile, delimiter=',', quotechar='|')
        data = list(reader)
    return data 

python - How to compare two rows in a csv file?, import csv with open('data.csv', 'r') as _: input = csv.reader(_, delimiter='\t') output = [[]] previous_col0 = None line_n = 0 for cols in input: if� This article shows the python / pandas equivalent of SQL join. You can find how to compare two CSV files based on columns and output the difference using python and pandas. The advantage of pandas is the speed, the efficiency and that most of the work will be done for you by pandas: * reading the CSV files(or any other) * parsing the information into tabular form * comparing the columns

The problem is that row is only accessible inside your for loop. If you want to read all the content of a csv you can just do this in your load_from_csv function:

def load_from_csv(filename):
    with open(filename,'r') as csvfile:
        return [row for row in csv.reader(csvfile)]

Python comparing CSV data with lists, How to compare two Excel files for differences - Duration: 7:25. Ablebits 479,093 views � 7:25 Duration: 3:11 Posted: 8 Jan 2015 Comparing two rows from a csv file in Python. Ask Question Asked 1 month ago. Active 1 month ago. Viewed 35 times -2. 0. I am trying to write a manhattan_distance

def load_from_csv(filename):    
    with open(filename,'r') as csvfile: 
        reader = csv.reader(csvfile, delimiter=',', quotechar='|')

        WHAT_TO_RETURN = [] #an empty list

        for row in reader:
            # print(row) # don't do that

            GET_RID_OF_SPACES = row.split() # this is now a list

            WHAT_TO_RETURN.append(GET_RID_OF_SPACES)

    return WHAT_TO_RETURN 

# this is a list which contains one list for every row in your csv
some_rows_of_data = load_from_csv('data.csv')

def manhattan_distance(x,y):
   return sum(abs(a-b) for a,b in zip(x,y))

my_function_result = manhattan_distance( some_rows_of_data[0],
                                         some_rows_of_data[1] )

print(my_function_result)

Something like this should work better. You cannot return something and then not store it when used. You had return row at the end of the load_from_csv.

However, when you used load_from_csv, you just ran it flat out in the file. You need to store the return value in a variable so that you can use it later on. i.e. store_it = load_from_csv("somefile.csv") Now, row is returned to store_it and saved in a variable.

Of course your load_from_csv function has other issues, but most important is to know what to use return for, and how to "catch" what is returned.

Happy Pythoning, and Happy New Year.

In Python, How do I read 2 CSV files, compare column 1 from both , I am looking for a Python way to compare the 2 CSV files (only Column 1), similarly if you want want to compare multiple columns then just� Have two CSV files containing client records and need to compare the two and then output to a third file those rows where there are differences to the values within the record (row) as well as output those records (rows) on the second file that are not on first file .

csvdiff � PyPI, Generate a diff between two CSV files. you to compare the semantic contents of two CSV files, ignoring things like row and You'll firstly need Python and pip. csv-diff. Tool for viewing the difference between two CSV files. See Generating a commit log for San Francisco’s official list of trees (and the sf-tree-history repo commit log) for background information on this project. Installation pip install csv-diff Usage. Consider two CSV files: one.csv. id,name,age 1,Cleo,4 2,Pancakes,2 two.csv

csv-diff � PyPI, Python CLI tool and library for diffing CSV files. csv-diff one.csv two.csv --key= id 1 row changed, 1 row added, 1 row removed 1 row changed Row 1 age: "4"� import csv import sys f = open(sys.argv[1], ‘rb’) reader = csv.reader(f) for row in reader print row f.close() In the first two lines, we are importing the CSV and sys modules. Then, we open the CSV file we want to pull information from. Next, we create the reader object, iterate the rows of the file, and then print them.

Python: How can I compare two lists in CSV files?, How do I load CSV file into PostgreSQL database using Python? script to compare two CSV files having multiple columns and store differences in other files? A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. For working CSV files in python, there is an inbuilt module called csv. Reading a CSV file

Comments
  • @kiwi1498 Please look at creating a minimal reproducible example. In this case, a sample of what the csv contains (maybe the first 5 or 10 lines) would be helpful.
  • See this lovely debug blog for help. Insert useful output statements to trace the control and data flow. Remove inapplicable code; reduce working code to a hard-coded result. As the posting guidelines say, "make it easy for others to help you." Most of all, this will show you where you've made your errors.
  • thanks, I forgot about that bit, it's there now.
  • Row is not set outside of the function. The row variable is only in the memory (scope) of the function. If you use row = load_from_csv('data.csv') it will create a variable named row outside of the function which captures the row data from the function.
  • Most of all, trace your function usage. Your function opens the file, reads and prints all of the rows, and returns only the last row. Your main program then ignores the return value. You have not stored anything to compare later. Please work through tutorials on basic data structures and CSV input.
  • Thank you so much, this is exactly what I was looking for. I knew I had to do something along those lines but as I mentioned, I am new to programming so I was not sure how to implement something like this in my code.
  • Thank you, this is very helpful.