Home > Net >  python returns an error that I cannot see SyntaxError: invalid syntax
python returns an error that I cannot see SyntaxError: invalid syntax

Time:07-28

I am writing a program that counts individuals in the DF and divides them by race. The problem is that python is giving me an error which I don't think makes sense. This is my code:

dic_of_race = {"WBP":10, "PBZ":20, "PUL":40,"HAMP":60, "DUROC":70, "PIT":80, "990":90}
list_of_df = [DFl,DFr,DFm,DFk]

completeDF = pd.DataFrame()
for df in list_of_df:
    for race in dic_of_race:
        completeDF['race'] = race
        if df == DFl:
            completeDF['individual'] = 'fpig'
            femaleL = len(df[(df.race== dic_of_race[race])
        elif df == DFr:
            completeDF['individual'] = 'fpig'
            femaleR = len(df[(df.race== dic_of_race[race])
        elif df == DFk:
            completeDF['individual'] = 'knury'
            completeDF['quantity'] = len(df[(df.rasa == dic_of_race[race])
        elif df == DFm:
            if df['kp'] == 1:
                completeDF['individual'] = 'youngp'
                completeDF['quantity'] = len(df[(df.rasa == dic_of_race[race])
            elif df['kp'] == 2:
                completeDF['individual'] = 'youngpl'
                completeDF['quantity'] = len(df[(df.rasa == dic_of_race[race])

This is an error:

   elif df == DFr:
    ^
SyntaxError: invalid syntax

I don't know what's wrong. Elif seems to be correct.

CodePudding user response:

Every time you have len(df[(df.race== dic_of_race[race]) it should be len(df[(df.race== dic_of_race[race])]) - you have missed the last ]) each time.

  • Related