0

I have a dataframe containing election data of four different years. Column "Votes" contain the total votes a party got for different constituencies in each year. I need to find the winning party (party who has got maximum total votes) of each year. I have grouped the data using "Election year" and "Party". Now how can I get the Election Year and Party for the above case?

df1 = df.groupby(['Election Year', 'Party']).sum()
print(df1.loc[df1['Votes'].idxmax()])

The above code is not giving the expected result.

I have attached the Dataframe after using groupby

How can I get the expected result. Any suggestions is appreciated.

Deepak
  • 59
  • 1
  • 8
  • It might be worth reading [how to make a good pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). What is your expected result? – tomjn Jun 09 '21 at 10:09
  • I want to get the "Party" name getting maximum votes for a particular year. So, there should be four Parties as there are 4 different years in the dataframe. – Deepak Jun 09 '21 at 10:23

0 Answers0