w3resource

Pandas: Sort movies on runtime in descending order


12. Sort by Runtime Descending

Write a Pandas program to sort movies on runtime in descending order.

Sample Solution:

Python Code :

import pandas as pd
df = pd.read_csv('movies_metadata.csv')
small_df = df[['title', 'release_date', 'budget', 'revenue', 'runtime']]
#Sort Movies based on runtime (in descending order)
result = small_df.sort_values('runtime', ascending=False)
print("DataFrame sort on Runtime.")
print(result.head())

Sample Output:

DataFrame sort on Runtime.
                    title release_date    budget    revenue  runtime
13                  Nixon   1995-12-22  44000000   13681765    192.0
15                 Casino   1995-11-22  52000000  116112375    178.0
5                    Heat   1995-12-15  60000000  187436818    170.0
16  Sense and Sensibility   1995-12-13  16500000  135000000    136.0
22              Assassins   1995-10-06  50000000   30303072    132.0  
	                                       

For more Practice: Solve these Related Problems:

  • Write a Pandas program to load movies_metadata.csv and sort the movies in descending order based on the runtime column.
  • Write a Pandas program to display movies_metadata.csv sorted by runtime (longest to shortest) and show the title and runtime.
  • Write a Pandas program to convert runtime to numeric (if necessary) and sort movies_metadata.csv in descending order.
  • Write a Pandas program to sort movies by runtime in descending order and then filter out movies with runtime under 60 minutes.

Go to:


Previous: Write a Pandas program to access those movies,released after 1995-01-01.
Next: Write a Pandas program to get those movies whose revenue more than 2 million and spent less than 1 million.

Python-Pandas Code Editor:

Sample Table:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.