lundi 7 septembre 2020

How do I check for AND and OR conditions in a python pandas data frame column of type "Array"?

I have created a function to check for each values in the array column of a pandas data frame and then assign values to a new column accordingly. Basically predicting if a diet is healthy based on food intake.

Function I wrote:

Creates a new column called "diet_status" and assigns the value based on values in the column "food_intake" which is of type array.

 def diet(a):
    if 'fruits' in a:
        y = 'healthy'
    elif 'vegetables' in a:
        y = 'healthy'
    elif 'chips' in a:
        y = 'unhealthy'
    elif 'sweets' in a:
        y = 'unhealthy'
    else
        y = 'NA'
    return y 



df["diet_status"] = df["food_intake"].apply(diet)

How do I check for multiple conditions here? For example, if the "food_intake" array contains "(fruits AND vegetables) AND (chips OR sweets)", I want to name it as "balanced". Need to basically check for AND/OR conditions of values in the array. Could anyone please help me on this.

Aucun commentaire:

Enregistrer un commentaire