Create a function in python that returns a number that is the number of equal values of two columns

1

I'm trying to create a function that returns me a number that is the amount of equal values in two columns. (I'm new to programming)

I tried the following (I do not know how wrong it is):

e = 0
df_08['cmb_mpg'] = A
df_18['cmb_mpg'] = B
def equal():
    while e < A(len):
        for b in 'B': 
            for a in 'A':
                if a == b:
                    e = e+1
return e 
    
asked by anonymous 29.01.2018 / 23:25

1 answer

2

Well, you have some syntax errors in your code. I'll try to list them.

1 - To declare a variable of type string just do:

nomeVariavel = "conteúdo da string"

2nd - Your function is not receiving any parameters. The function must receive parameters, which will be the values used within the function. Ex.:

def quantosIguais(a, b):

3 - The variant and , which will be used as a counter, must be declared within the scope of the function.

4º - To find the string size, it is done len(nomeString) and not (len)nomeString .

5º - In the for loops you used, they cover only 1 value, which is the letter you put. To use the variable, simply place the variable name after in . Using the quotation marks, you go through a new string, different from the string stored in the variable. Make:

for a in nomeString

instead of:

for a in 'nomeString'

Well, I've tried listing the syntax errors, but as has been said, your code does not make sense. I would also like to understand what your logic was to get to that code.

I'll leave an example of a function that does what you tried to do:

def quantasIguais(strA, strB):
    e = 0
    for i in range(len(strA)):
        if strA[i] == strB[i]:
            e += 1
    return e
    
30.01.2018 / 00:04