-1

I need to compare a list of integers values with a single row in a CVS file in order to find which line matches that values.

'''

firstScore = 90  
secondScore = 80  
thirdScore = 75  

list = [firstScore, secondScore, thirdScore]

'''

and the csv data is:

    Name,first,second,third
    Paul,40,60,30
    Kevin,90,80,75
    Jenny,80,75,90

The actual output should be the name which matches the 3 values: Kevin.

jovcode
  • 47
  • 12

3 Answers3

0

Since your code is lacking I assumed you make use of the pandas package with read_csv to create a dataframe (df) with the data given above. If so you can use the following to retrieve data from your df:

 df.loc[(df['first'] == list[0]) & (df['second'] == list[1]) & (df['third'] == list[2])].iloc[:,0]
Burbs
  • 72
  • 6
0

I'm not clear on where you data is coming from, but this code should cover the key logic:

import io

data = '''
[Name, first, second, third]
[Paul, 40, 60, 30]
[Kevin, 90, 80, 75]
[Jenny, 80, 75, 90]
'''

lstFind = [str(x) for x in [90, 80, 75]] # convert to string fields

lstAll = []

f = io.StringIO(data)  # load string data as file text
for row in f: 
  if not row.strip(): continue # skip empty rows
  rowlst = row.strip().replace(" ","")[1:-1].split(",") # convert row to list
  lstAll.append(rowlst) # add to main list

for lst in lstAll:  # each list 
   if lst[1:] == lstFind:
      print(lst[0])    # Kevin

  
Mike67
  • 11,175
  • 2
  • 7
  • 15
0

Thanks to Handler (https://stackoverflow.com/users/9659528/handler ) who has solved it while this question was closed. ''' import csv

list = [firstScore, secondScore, thirdScore]

with open('test.csv', 'rt') as f:
reader = csv.reader(f, delimiter=',')

# skip the header of your csv
next(reader)

for row in reader:
  if((list[0] == int(row[1])) and (list[1] == int(row[2])) and (list[2] == int(row[3]))):
    # print name (present in first column -> index 0 of the row) 
    print(row[0])
    break
  else:
    print("No match found..")

'''

jovcode
  • 47
  • 12