Home > Software design >  Python - Compare last column in all rows of a .csv file
Python - Compare last column in all rows of a .csv file

Time:01-02

I have read all the rows from a .csv file like this:-

from csv import reader, writer

# open file in read mode
with open('data.csv', 'r') as read_obj:

    # pass the file object to reader() to get the reader object
    csv_reader = reader(read_obj)
    # Iterate over each row in the csv using reader object
    for row in csv_reader:

Now, I am trying to compare only the last column of each row in the csv file. The data in the csv file is like this:-

Row 1: 0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,0,0,0,1
Row 2: 0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,0,0,0,0
Row 3: 0,1,0,1,1,1,0,1,0,0,1,0,0,0,1,1,1,0,1,0,0,0,1
Row 4: 0,1,0,1,1,1,0,1,0,0,1,0,0,0,1,1,1,0,1,0,0,0,1
Row 5: 0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,0,0,0,0

I have to compare only the last column of each row for 1 and 0. In the last column, if there is a 1 in 1st row and a 0 in the 2nd row, then we print 1st row.

For ex 1:

Row 1: 0,0,1,1
Row 2: 0,1,1,0

Then, I print Row 1

For ex 2:

Row 1: 0,0,1,1
Row 2: 0,1,1,1
Row 3: 0,1,1,0

Then, I print Row 2

Would appreciate any suggestions for this problem. Thanks!

CodePudding user response:

Use Pandas

import pandas as pd

path = (r"data.csv")

data = pd.read_csv(path)

last_column = data.iloc[: , -1]

CodePudding user response:

I hope this helps. It shows you how to:

  1. read your input rows, convert from '1' and '0' to 1 and 0, and store the row
  2. look up the last and second-to-last rows
  3. compare the last columns of those two rows
import csv

all_rows = []
with open('data.csv', newline='') as in_f:
    reader = csv.reader(in_f)

    # Read all rows and convert columns from string to int
    for row in reader:
        all_rows.append([int(col) for col in row])

last_row = all_rows[-1]
previous_row = all_rows[-2]

if previous_row[-1] == 1 and last_row[-1] == 0:
    with open('new.csv', 'w', newline='') as out_f:
        writer = csv.writer(out_f)
        writer.writerow(previous_row)

When I run that on Ex 1:

0,0,1,1
0,1,1,0

I get:

0,0,1,1

On Ex 2:

0,0,1,1
0,1,1,1
0,1,1,0

I get

0,1,1,1

And on the big example:

0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,0,0,0,1
0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,0,0,0,0
0,1,0,1,1,1,0,1,0,0,1,0,0,0,1,1,1,0,1,0,0,0,1
0,1,0,1,1,1,0,1,0,0,1,0,0,0,1,1,1,0,1,0,0,0,1
0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,0,0,0,0

I get Row 4:

0,1,0,1,1,1,0,1,0,0,1,0,0,0,1,1,1,0,1,0,0,0,1
  • Related