Home > Software engineering >  How to search row by 2 columms
How to search row by 2 columms

Time:08-17

Hey i have been trying for day's to search row in data by 2 inputs i manage to find row by any 1 data that i type

For example if i have list of people who live in United state and their job and i have "bob" "thebuilder" then i want to search up "bob" "thebuilder" and i get the full row of bob job and state, for now what i got i can only type one indicaitor like "Bob" or "thebuilder" and look up from all the rows that got printed,

That's what i got

    def searchByName():
    name=input('Insert name:')
    csv_file=csv.reader(open('names.csv', encoding="utf8"))
    
    
    for row in csv_file:
        if name==row[3]:
            print(row)

and I tried to do it like this

enter cdef searchByName():
fname=input('Insert name:')
lname=input('enter last name:')
csv_file=csv.reader(open('names.csv', encoding="utf8"))


for row in csv_file:
    if fname==row[3]   lname==row[4]:
        print(row)

when run like this i get no print its loading but get

Process finished with exit code 0

in csv file i got the first names in row 3 and last name at row 4,

What i'm missing? search it up all google even tried for hours maybe to comabin it on excel but file to big, How can i manage to look up that exact row i want just instead by 1 input from 2, Thank you!

CodePudding user response:

you can filter for where a DataFrames columns hold specific values by putting those conditions into square brackets after the DataFrames name, like you slicing a list

enter cdef searchByName():
fname=input('Insert name:')
lname=input('enter last name:')
csv_file=csv.reader(open('names.csv', encoding="utf8"))

#assuming your columns names are 0,1,2,3,4....
print(csv_file[(csv_file[3] == fname) & (csv_file[4] == lname)])
    

CodePudding user response:

Any help? please? trying to maybe do it with pandas but go 0 knowledge

  • Related