How to make new dataframe from existing dataframe by removing duplicates from columns?

Question:

I have a dataframe ‘merged_df’ that looks like this:

Login ID Volume
cab001 4
cab002 3
cab001 4
cab003 2

There are many duplicates in the login_id column. I want to make another dataframe with only unique ‘login_id’ and the sum of ‘volume’ for each unique ‘login_id’.

Asked By: Atif

||

Answers:

Will this get you what you want?

df = pd.DataFrame({
    'login_id' : [1, 1, 2, 2, 3],
    'Volumn' : [10, 10, 20, 20, 50]
})

df_new = df.groupby('login_id', as_index = False)['Volumn'].sum().sort_values('Volumn', ascending = False)
Answered By: ArchAngelPwn
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.