Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value of a column based on the maximum of another column in case of DataFrame.groupby

I have a dataframe which looks like this.

id YearReleased Artist count 168 2015 Muse 1 169 2015 Rihanna 3 170 2015 Taylor Swift 2 171 2016 Jennifer Lopez 1 172 2016 Rihanna 3 173 2016 Underworld 1 174 2017 Coldplay 1 175 2017 Ed Sheeran 2

I want to get the maximum count for each year and then get the corresponding Artist name.

Something like this:

YearReleased Artist

2015 Rihanna
2016 Rihanna
2017 Ed Sheeran

I have tried using a loop to iterate over the rows of the dataframe and create another dictionary with key as year and value as artist. But when I try to convert that dictionary to a dataframe, the keys are mapped to columns instead of rows.

Can somebody guide me to have a better approach to this without having to loop over the dataframe and instead use some inbuilt pandas method to achieve this?

like image 923
Jeet Banerjee Avatar asked Mar 13 '18 18:03

Jeet Banerjee


People also ask

How do I get the value of a column based on another column value?

Use pandas. DataFrame. query() to get a column value based on another column. Besides this method, you can also use DataFrame.

How do you find the maximum value of a column in a DataFrame?

The max() method returns a Series with the maximum value of each column. By specifying the column axis ( axis='columns' ), the max() method searches column-wise and returns the maximum value for each row.

How do you get Groupby index in pandas?

How to perform groupby index in pandas? Pass index name of the DataFrame as a parameter to groupby() function to group rows on an index. DataFrame. groupby() function takes string or list as a param to specify the group columns or index.


1 Answers

Look at idxmax

df.loc[df.groupby('YearReleased')['count'].idxmax()]
Out[445]: 
    id  YearReleased     Artist  count
1  169          2015    Rihanna      3
4  172          2016    Rihanna      3
7  175          2017  EdSheeran      2
like image 127
BENY Avatar answered Oct 15 '22 17:10

BENY