Skip to content Skip to sidebar Skip to footer

Calculate Points With Pandas

I'm stuck on how to get to my destination. I have students points. I'd like to code it so that out of the total subjects, a students points can only be calculated out of a selected

Solution 1:

df['POINTS'] = df['ENG'] + df['KIS'] + df[['BIO', 'PHY']].max(axis=1) + df[['HIS', 'GEO', 'CRE']].max(axis=1)

 ENG  KIS  BIO  PHY  HIS  GEO  CRE  POINTS
  1091157283835331273235749966306961071183631042871027

Solution 2:

In order to take the sum you need to take use aggregation functions in pandas :

  sums = df['ENG'].sum()+df['KIS'].sum()+df['BIO'].sum()+df['PHY'].sum()+df['HIS'].sum()+df['GEO'].sum()+df['CRE'].sum()

That way you will have sums of all the items in the sums variable and than in your if condition each one would be like :

sum = (sums) - (df['BIO'].sum())

So all your if conditions would look like this :

ifdf['BIO'].sum() >= df['PHY'].sum():
   sum = (sums) - (df['PHY'].sum())
 else:
   sum = (sums) - (df['BIO'].sum())
 ifdf['GEO'].sum() >= df['CRE'].sum():
   sum = (sums) - (df['CRE'].sum())
 else:
   sum = (sums) - (df['GEO'].sum())
 ifdf['CRE'].sum() >= df['HIS'].sum():
   sum = (sums) - (df['CRE'].sum())
 else:
   sum = (sums) - (df['HIS'].sum())
 df['POINTS'] = sumprint(df)

This is based on what I understood, I am not sure if you are comparing the sums or just individual values.

Post a Comment for "Calculate Points With Pandas"