简体   繁体   中英

Calculate a score for each row based on multiple conditions in pandas avoiding a for loop

I want to calculate a score based on multiple column conditions. At this moment I'm using iterrows and a for loop to calculate the value but I was wondering if there is a more efficient way to achieve the same result avoiding a for loop?

import pandas as pd
df = pd.DataFrame([{'column1':1, 'column2':['low grade'],'column3': True}, {'column1':8,'column2':['low grade', 'medium grade'],'column3': False}, {'column1': 7,'column2':['high grade'],'column3': True}])
for index, resource in df.iterrows():
    i = 0
    i = i + df.apply(lambda x: 0 if (x['column1'] == 1)
                                        else (3 if x['column1'] > 1 and x['column1'] < 8
                                        else (6 if x['column1'] >=8
                                        else 0)), axis=1)  
    i = i + df.apply(lambda x: 1 if ("high" in str(x['column2']))
                                         else (2 if "low" in str(x['column2'])
                                        else 0), axis=1) 
    i = i + df.apply(lambda x: 1 if (x['column3'] == True)
                                        else 0, axis=1)   
    df["score"] = i

    df["critical"] = df.apply(lambda x: True if ((x['score'] < 5) and 
    ("low" in str(x['column2'])  or
    "high" in str(x['column2'])  ))
                                        else False, axis=1)   

You could do something like this:

def calculate_score(x):
    score = 0
    if x['column1'] > 1 and x['column1'] < 8 :
        score += 3
    elif x['column1'] >=8 :
        score += 6
    else:
        score += 0
        
    if "high" in str(x['column2']) :
        score += 1
    elif "low" in str(x['column2']) :
        score += 2
    else:
        score += 0
        
    if x['column3'] == True:
        score += 1
    else:
        score += 0
        
    return score

df['score'] = df.apply(calculate_score, axis=1)
df["critical"] = df.apply(lambda x: True if ((x['score'] < 5) and ("low" in str(x['column2'])  or "high" in str(x['column2']))) else False, axis=1)

This way you could avoid iterrows and replace multiple apply with a single apply .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM