0

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.

3
  • 3
    The problem is with your previous line, you missed a ]) Commented Jul 27, 2022 at 10:04
  • Also if you upgrade to Python 3.10 you would get better syntax errors Commented Jul 27, 2022 at 10:05
  • @El. I have version 3.10 already Commented Jul 27, 2022 at 10:44

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.