porschewitz
porschewitz

Reputation: 73

Pandas groupby throwing KeyError

Pandas throws a KeyError for this code even though all columns exist in the dataframe and I have already checked for spaces in the name:

violations_zipcode = final_df.groupby([final_df['FACILITY ZIP', 'FACILITY NAME']])['VIOLATION DESCRIPTION'].count().reset_index(name='No. Violations').sort_values(by='No. Violations', ascending=False)

Here's what the dataframe looks like:

     VIOLATION DESCRIPTION     FACILITY ZIP  FACILITY NAME
0    Poor hygiene              90210         Starbucks
1    Lack of seating           70024         Domino's
2    Insufficient ventilation  34567         Geppetto Pizza
3    Rodents                   00345         Taco Bell
4    .....                    .....         .....

What can it be?

Upvotes: 1

Views: 285

Answers (1)

adhg
adhg

Reputation: 10853

Your group by syntax is incorrect. It should be

final_df.groupby(['FACILITY ZIP', 'FACILITY NAME'])['VIOLATION DESCRIPTION'].count()...

Check groupby syntax:

Upvotes: 1

Related Questions