mercredi 28 juin 2017

Python: Create New Field in Dataframe Using Complex Conditional Summing Logic

Below is my table (Python dataframe). I'm trying to create the last column in purple text.

enter image description here

Below is the logic I want to implement:

  1. For each unique 'cbsa' value, if the associated 'zip' field values are all the same then set 'age_HC01_EST_VC31_2' field equal to 'age_HC01_EST_VC31' field (see rows highlighted in yellow).

  2. For each unique 'cbsa' value, if the associated 'zip' field values are different then set 'age_HC01_EST_VC31_2' field equal to the sum of 'age_HC01_EST_VC31' field values (see rows highlighted in orange).

  3. For each unique 'cbsa' value, if the associated 'zip' field values are some the same and some different, then set 'age_HC01_EST_VC31_2' field equal to the sum of UNIQUE 'age_HC01_EST_VC31' field values (see rows highlighted in blue).

I have tried using groupby and then sum on 'cbsa' field ... but it doesn't work for the specific, multi-layered logic I'm trying to implement.

Any help is greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire