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’.
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)
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’.
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)