pradeepsf729 / pandas_101

Pandas questions with solutions
1 stars 0 forks source link

SORT #14

Closed sandeepny441 closed 9 months ago

sandeepny441 commented 1 year ago

SORT_Questions

import pandas as pd

Sample data

data = { 'Player': ['Roger Federer', 'Rafael Nadal', 'Novak Djokovic', 'Serena Williams', 'Margaret Court', 'Arthur Ashe', 'Andre Agassi', 'Pete Sampras', 'Martina Navratilova', 'Chris Evert', 'Steffi Graf', 'Rod Laver'], 'Total_Slams': [20, 20, 20, 23, 24, 3, 8, 14, 18, 18, 22, 11], 'Wimbledon': [8, 2, 6, 7, 3, 1, 1, 7, 9, 3, 7, 4], 'US_Open': [5, 4, 3, 6, 5, 1, 2, 5, 4, 6, 5, 2], 'Australian_Open': [6, 1, 9, 7, 11, 1, 4, 2, 3, 2, 4, 3], 'French_Open': [1, 13, 2, 3, 5, 0, 1, 0, 2, 7, 6, 2], }

tennis_df = pd.DataFrame(data)

  1. How would you sort the DataFrame based on the "Total_Slams" column in descending order?
  2. Can you sort the DataFrame by the players' names in alphabetical order? What method would you use?
  3. Is it possible to sort the DataFrame based on multiple columns? For instance, sort by "Total_Slams" first and then by "Wimbledon" titles.
  4. How would you sort the DataFrame based on the "French_Open" column but keep the original index?
  5. Can you perform an in-place sort on the "US_Open" titles, so the original DataFrame is modified?
  6. What is the syntax for sorting the DataFrame based on "Australian_Open" in ascending order but returning only a subset of the DataFrame instead of modifying it?
  7. How would you reset the index after sorting the DataFrame based on "Total_Slams"?
  8. Is it possible to sort just a Series extracted from the DataFrame? For example, sort the "Wimbledon" Series.
  9. How can you sort the DataFrame by the index after you've sorted it by a column?
  10. Can you sort the DataFrame by "Total_Slams" in descending order but display only the top 5 players?
pradeepsf729 commented 1 year ago

Fixed in https://github.com/pradeepsf729/pandas_101/pull/15