-1

So I'm writing code to using pandas to pull Excel sheets into a dataframe and do operations on them from there one of these operations is that it should see if 2 columns values in one data frame match 2 column values in another data frame initially I was using the " if .. in " function this worked to find if values of one column of df1 was in the df2 then I tried to check if the second column values match but that did not work. I tried nesting it but that gave wrong info. So I tried iterating through it using for loops as that would need both conditions need to be met but learned that iterating in pandas isn't too good and very slow so that idea was scrapped what should I do .

for person in people['Name'].values:
    sport = list[list['Name'] == person]
    listr = []
    if sport.empty:
        print( person, ' does not do a sport')
    else:
        for req in result['Title'].values:
            if req in sport['Title'].values:
                listr.append('x')
            else:
                listr.append('\n')

This code here works for 1 column match but when I nest another for loop in here to check column 2 if column 1 matches the list size is not what it should be I think becauese the in function checks the whole df and not row by row so is there a way to do this

Ok this is some random stuff to illustrate my

excel 1

Name Sports Points
david Soccer 12
Jessy footbal 32
kelcy fencing 5
jason diving 13
jason soccer 14

excel 2

sport points
basketball 12
soccer 14
football 7
fencing 7
shotput 8
archery 3
diving 13

DF 'Jason'

Name sport points
jason diving 13
jason soccer 14

so right now I have code that goes through excel 1 pulls out the names i want to work with then puts all that info in new dataframe such that all names have their own dataframe. Then excel2 is put into a new Dataframe. So the sports columns of the person dataframe and the excel 2 dataframe are compared and results r put into a list like this [ 0,x,0,0,0,0,x] x when there is a match null or 0 when there is no match i want to compare both the sports and the points so that it gives a different answer when both point and sport matches and when just sport matches so in this case it would give [0,x,0,0,0,0,Y] and ideas? sorry if this makes no sense

0 Answers0