mardi 25 mai 2021

Assign category based on boolean condition

I'm trying to figure out if there's a better way to categorize data based on a condition.

Example Data: Seeing if identified places have physical, social, and/or economic roles. If any/many of the roles are present, the place is marked with "1".

import pandas as pd
df = pd.DataFrame([[0, 1, 0], [0, 1, 1], [0, 1, 0], [0, 0, 1], [1,1,1], [1,1,0], columns=["PHYSICAL", "SOCIAL", "ECONOMIC"])

Data

|   | PHYSICAL | SOCIAL | ECONOMIC | 
| - | -------- | ------ | -------- | 
| 0 | 0        | 1      | 0        | 
| 1 | 0        | 1      | 1        | 
| 2 | 0        | 1      | 0        | 
| 3 | 0        | 0      | 1        | 
| 4 | 1        | 1      | 1        |       
| 5 | 1        | 1      | 0        |      

What I Want to Know: How to make a new column that assigns each row a category based on True/False values.

All Possible Categories:

  • Physical (Only)
  • Social (Only)
  • Economic (Only)
  • Physical & Social
  • Physical & Economic
  • Social & Economic
  • Physical, Social, & Economic (All)

Expected Results

|   | PHYSICAL | SOCIAL | ECONOMIC | CATEGORY        |
| - | -------- | ------ | -------- | --------------- |
| 0 | 0        | 1      | 0        | social          |
| 1 | 0        | 1      | 1        | social_economic |
| 2 | 0        | 1      | 0        | social          |
| 3 | 0        | 0      | 1        | economic        |
| 4 | 1        | 1      | 1        | all_cat         |
| 5 | 1        | 1      | 0        | physical_social |

Thank you!

Aucun commentaire:

Enregistrer un commentaire