Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading