vendredi 8 janvier 2021

Applying Condition in Pandas DataFram on more than one column

Kindly help me with the following code

import numpy as np
import pandas as pd
np.random.seed(2)
NumberOfStudents=10

MNum=np.random.randint(40,95,NumberOfStudents)
SNum=np.random.randint(40,95,NumberOfStudents)
ENum=np.random.randint(40,95,NumberOfStudents)

df=pd.DataFrame({"Math":MNum,"Science":SNum,"English":ENum}, index=["R"+str(i+1) for i in range(10)])


df["Total"]=df["Math"]+df["Science"]+df["English"]
df["percent"]=df["Total"]/4

df.loc[(df['English'] < 50) or(df['Math'] < 50) or (df['Science'] < 50)  , 'Result'] = 'Fail'

df.loc[(df['English'] >= 50) and (df['Math'] >= 50) and (df['Science'] >= 50)  , 'Result'] = 'Pass'

The Last 2 lines cause error it doesn't allow me to apply condition on various column. however condition on one column works fine.

df.loc[df['English'] < 50  , 'Result'] = 'Fail'

Kindly help me improve this syntax

Aucun commentaire:

Enregistrer un commentaire