0

I have a data frame which looks as follows

df= pd.DataFrame(np.array([[1, 2, 3], [1, 5, 6], [1, 8, 9],[2, 18, 9],[3, 99, 10],[3, 0.3, 5],[2, 58, 78],[4, 8, 9]]),

                   columns=['id', 'point_A', 'point_B'])

enter image description here

Now I want to create column which is the sum of both point_A and point_B row . I can do that by this code: df["sum_of_all"] = df[["point_A","point_B"]].sum(axis = 1)

Now I want to give sort them based on sum_of_all. Meaning the most sum will be graded as 1 and so on. Now it has to be done based on id , How can I do that ?

Update :

enter image description here Once I have finished the sum and sorting I get the above output. Now My goal is to assigne grade based on id. i.e : id 2 ,index 6, -> grade = 1,id 2 in index 3 -> grade 2 , id 3 on index 4 -> grade 1 and id 3 on index 5 -> 2 and so on Thats the expection

0

1 Answer 1

2

IICU

df2=df.sort_values(by=['sum_of_all','id'], ascending=[False, False])
df2['grade']=df2.groupby('id')['sum_of_all'].cumcount()+1
df2

Outcome

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.