dancemc15
dancemc15

Reputation: 628

How do I drop rows with zeros in Panda dataframe?

I want to take the average property assessment value for each zipcode (without counting the zeros). So I would have an average value for 02126 and an average value for 02130. I would like to drop all rows with 0 in AV_LAND. What is the best way to go about that in a pandas dataframe? Thanks in advance!

   ZIPCODE AV_LAND
0   02130   7900
1   02126   0
2   02126   20600
3   02126   520800
4   02126   20800
5   02127   0

Upvotes: 1

Views: 1891

Answers (1)

piRSquared
piRSquared

Reputation: 294506

assuming df is your dataframe

df.loc[df.AV_LAND != 0].groupby('ZIPCODE').mean()

enter image description here

df.loc[df.AV_LAND != 0].groupby('ZIPCODE').mean().reset_index()

enter image description here

Upvotes: 2

Related Questions