Instagram
youtube
Facebook
Twitter

Sort a Pandas DataFrame in Descending Order by a Specific Column

    Sort a Pandas DataFrame in Descending Order by a Specific Column
     
     Short Description of the Program:

     This program reads a CSV file into a DataFrame and sorts it in descending order by the values in the 'Age' column.
     
      Explanation:

  • sort_values(by='Age', ascending=False)
    Sorts the DataFrame by 'Age' from highest to lowest.

     
  • You can change 'Age' to any other column (like 'Name' or 'City') for custom sorting.
     

Program:

import pandas as pd

df = pd.read_csv('data.csv')

sorted_df = df.sort_values(by='Age', ascending=False)

print(sorted_df)