w3resource

Pandas: Display the movies longer than 30 minutes and shorter than 360 minutes

Pandas: IMDb Movies Exercise-16 with Solution

Write a Pandas program to display the movies (title, runtime) longer than 30 minutes and shorter than 360 minutes.

Sample Solution:

Python Code :

import pandas as pd
df = pd.read_csv('movies_metadata.csv')
small_df = df[['title', 'runtime']]
result = small_df[(small_df['runtime'] >= 30) & (small_df['runtime'] <= 360)]
print("List of movies longer than 30 minutes and shorter than 360 minutes:")
print(result)

Sample Output:

List of movies longer than 30 minutes and shorter than 360 minutes:
                             title  runtime
0                        Toy Story     81.0
1                          Jumanji    104.0
2                 Grumpier Old Men    101.0
3                Waiting to Exhale    127.0
4      Father of the Bride Part II    106.0
5                             Heat    170.0
6                          Sabrina    127.0
7                     Tom and Huck     97.0
8                     Sudden Death    106.0
9                        GoldenEye    130.0
10          The American President    106.0
11     Dracula: Dead and Loving It     88.0
12                           Balto     78.0
13                           Nixon    192.0
14                Cutthroat Island    119.0
15                          Casino    178.0
16           Sense and Sensibility    136.0
17                      Four Rooms     98.0
18  Ace Ventura: When Nature Calls     90.0
19                     Money Train    103.0
20                      Get Shorty    105.0
21                         Copycat    124.0
22                       Assassins    132.0
23                          Powder    111.0
24               Leaving Las Vegas    112.0
25                         Othello    123.0
26                    Now and Then    100.0
27                      Persuasion    104.0
28       The City of Lost Children    108.0
29                  Shanghai Triad    108.0
30                 Dangerous Minds     99.0
31                  Twelve Monkeys    129.0
32                Wings of Courage     50.0
33                            Babe     89.0
34                      Carrington    121.0
35                Dead Man Walking    122.0
36          Across the Sea of Time     51.0
37                    It Takes Two    101.0
38                        Clueless     97.0
39        Cry, the Beloved Country    106.0
40                     Richard III    104.0
41                 Dead Presidents    119.0
42                     Restoration    117.0
43                   Mortal Kombat    101.0
44                      To Die For    106.0
45   How To Make An American Quilt    116.0
46                           Se7en    127.0
47                      Pocahontas     81.0
48           When Night Is Falling     96.0
49              The Usual Suspects    106.0	                                       

Python-Pandas Code Editor:

Sample Table:


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

Previous: Write a Pandas program to calculate the number of votes garnered by the 70% movie.
Next: Write a Pandas program to display the movies (title, number of votes) that received specified number of votes.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/pandas/movies/python-pandas-movies-exercise-16.php