PDA Assignments
  • Python For Data Analytics
    • 1.Python
      • 1.Python Documents
        • 1.Data Types
        • 2.Variables In Python
        • 3.Operators In Python
        • 4.User Input In Python
        • 5.TypeCasting In Python
        • 6.Strings In Python
        • 7.Conditional Statements In Python
        • 8.Branching using Conditional Statements and Loops in Python
        • 9.Lists In Python
        • 10.Sets In Python
        • 11.Tuples In Python
        • 12.Dictionary In Python
        • 13.Functions In Python
        • 14.File Handling In Python
        • 15.Numerical Computing with Python and Numpy
      • 2.Python Assignments
        • Data Type & Variables
        • Operators Assignment
        • User Input & Type Casting
        • Functions- Basic Assignments
        • String Assignments
          • String CheatSheet
        • Conditional Statements Assignments
        • Loops Assignments
        • List Assignments
          • List Cheatsheet
        • Set Assignments
          • Sets Cheatsheet
        • Dictionary Assignments
          • Dictionary Cheatsheet
        • Function Assignments
        • Functions used in Python
      • 3.Python Projects
        • Employee Management System
        • Hamming distance
        • Webscraping With Python
          • Introduction To Web Scraping
          • Importing Necessary Libraries
          • Basic Introduction To HTML
          • Introduction To BeautifulSoup
          • Flipkart Web Scraping
            • Scraping Step By Step
        • Retail Sales Analysis
        • Guess the Word Game
        • Data Collection Through APIs
        • To-Do List Manager
        • Atm-functionalities(nested if)
        • Distribution of Cards(List & Nested for)
        • Guess the Number Game
      • 4.Python + SQL Projects
        • Bookstore Management System
    • 2.Data Analytics
      • 1.Pandas
        • 1.Pandas Documents
          • 1.Introduction To Pandas
          • Reading and Loading Different Data
          • 2.Indexing and Slicing In Pandas
          • 3.Joining In Pandas
          • 4.Missing Values In Pandas
          • 5.Outliers In Pandas
          • 6.Aggregating Data
          • 7.DateTime In Pandas
          • 8.Validation In Pandas
          • 9.Fetching Data From SQL
          • 10. Automation In Pandas
          • 11.Matplotlib - Data Visualization
          • 12. Seaborn - Data Visualization
          • 13. Required Files
        • 3.Pandas Projects
          • Retail Sales Analysis
            • Retail Sales Step By Step
          • IMDB - Dataset Analysis - Basic
        • 2. Pandas Assignments
          • 1. Reading and Loading the Data
          • 2. Data frame Functions and Properties
          • 3. Series - Basic Operations
          • 4. Filtering in Pandas
          • 5. Advance Filtering
          • 6. Aggregate Functions & Groupby
          • 7. Pivot Tables
          • 8. Datetime
          • 9. String Functions
Powered by GitBook
On this page
  1. Python For Data Analytics
  2. 2.Data Analytics
  3. 1.Pandas
  4. 2. Pandas Assignments

9. String Functions

Previous8. Datetime

Last updated 2 months ago

  1. Read the file and create a new column called title-length containing the length of each movie.

Solution
import pandas as pd

df=pd.read_csv('IMDB Top 250 Movies.csv')
df['title_length']=df['name'].str.len()
df

# Output
        rank	name	year	rating	genre	certificate	run_time	tagline	budget	box_office	casts	directors	writers	titale_length	title_length
0	1	The Shawshank Redemption	1994	9.3	Drama	R	2h 22m	Fear can hold you prisoner. Hope can set you f...	25000000	28884504	Tim Robbins,Morgan Freeman,Bob Gunton,William ...	Frank Darabont	Stephen King,Frank Darabont	24	24
1	2	The Godfather	1972	9.2	Crime,Drama	R	2h 55m	An offer you can't refuse.	6000000	250341816	Marlon Brando,Al Pacino,James Caan,Diane Keato...	Francis Ford Coppola	Mario Puzo,Francis Ford Coppola	13	13
2	3	The Dark Knight	2008	9.0	Action,Crime,Drama	PG-13	2h 32m	Why So Serious?	185000000	1006234167	Christian Bale,Heath Ledger,Aaron Eckhart,Mich...	Christopher Nolan	Jonathan Nolan,Christopher Nolan,David S. Goyer	15	15
3	4	The Godfather Part II	1974	9.0	Crime,Drama	R	3h 22m	All the power on earth can't change destiny.	13000000	47961919	Al Pacino,Robert De Niro,Robert Duvall,Diane K...	Francis Ford Coppola	Francis Ford Coppola,Mario Puzo	21	21
4	5	12 Angry Men	1957	9.0	Crime,Drama	Approved	1h 36m	Life Is In Their Hands -- Death Is On Their Mi...	350000	955	Henry Fonda,Lee J. Cobb,Martin Balsam,John Fie...	Sidney Lumet	Reginald Rose	12	12
...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...
245	246	The Help	2011	8.1	Drama	PG-13	2h 26m	Change begins with a whisper.	25000000	216639112	Viola Davis,Emma Stone,Octavia Spencer,Bryce D...	Tate Taylor	Tate Taylor,Kathryn Stockett	8	8
246	247	Dersu Uzala	1975	8.2	Adventure,Biography,Drama	G	2h 22m	There is man and beast at nature's mercy. Ther...	4000000	14480	Maksim Munzuk,Yuriy Solomin,Mikhail Bychkov,Vl...	Akira Kurosawa	Akira Kurosawa,Yuriy Nagibin,Vladimir Arsenev	11	11
247	248	Aladdin	1992	8.0	Animation,Adventure,Comedy	G	1h 30m	Wish granted! (DVD re-release)	Not Available	Not Available	Scott Weinger,Robin Williams,Linda Larkin,Jona...	Ron Clements,John Musker	Ron Clements,John Musker,Ted Elliott	7	7
248	249	Gandhi	1982	8.0	Biography,Drama,History	PG	3h 11m	His Triumph Changed The World Forever.	22000000	52767889	Ben Kingsley,John Gielgud,Rohini Hattangadi,Ro...	Richard Attenborough	John Briley	6	6
249	250	Dances with Wolves	1990	8.0	Adventure,Drama,Western	PG-13	3h 1m	Inside everyone is a frontier waiting to be di...	22000000	424208848	Kevin Costner,Mary McDonnell,Graham Greene,Rod...	Kevin Costner	Michael Blake	18	18
250 rows × 15 columns
  1. Find the movie with the longest name.

Solution
ndf=df.loc[df['title_length']==max(df['title_length'])]
ndf

# Output
rank	name	year	rating	genre	certificate	run_time	tagline	budget	box_office	casts	directors	writers	titale_length	title_length
67	68	Dr. Strangelove or: How I Learned to Stop Worr...	1964	8.4	Comedy,War	PG	1h 35m	The comedy classic from celebrated director ST...	1800000	9523464	Peter Sellers,George C. Scott,Sterling Hayden,...	Stanley Kubrick	Stanley Kubrick,Terry Southern,Peter George	68	68
  1. Display the movies of drama genre.

Solution
ndf=df.loc[df['genre'].str.contains('Drama')]
ndf

# Output
        rank	name	year	rating	genre	certificate	run_time	tagline	budget	box_office	casts	directors	writers	titale_length	title_length
0	1	The Shawshank Redemption	1994	9.3	Drama	R	2h 22m	Fear can hold you prisoner. Hope can set you f...	25000000	28884504	Tim Robbins,Morgan Freeman,Bob Gunton,William ...	Frank Darabont	Stephen King,Frank Darabont	24	24
1	2	The Godfather	1972	9.2	Crime,Drama	R	2h 55m	An offer you can't refuse.	6000000	250341816	Marlon Brando,Al Pacino,James Caan,Diane Keato...	Francis Ford Coppola	Mario Puzo,Francis Ford Coppola	13	13
2	3	The Dark Knight	2008	9.0	Action,Crime,Drama	PG-13	2h 32m	Why So Serious?	185000000	1006234167	Christian Bale,Heath Ledger,Aaron Eckhart,Mich...	Christopher Nolan	Jonathan Nolan,Christopher Nolan,David S. Goyer	15	15
3	4	The Godfather Part II	1974	9.0	Crime,Drama	R	3h 22m	All the power on earth can't change destiny.	13000000	47961919	Al Pacino,Robert De Niro,Robert Duvall,Diane K...	Francis Ford Coppola	Francis Ford Coppola,Mario Puzo	21	21
4	5	12 Angry Men	1957	9.0	Crime,Drama	Approved	1h 36m	Life Is In Their Hands -- Death Is On Their Mi...	350000	955	Henry Fonda,Lee J. Cobb,Martin Balsam,John Fie...	Sidney Lumet	Reginald Rose	12	12
...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...
242	243	Persona	1966	8.1	Drama,Thriller	Not Rated	1h 23m	A new film by Ingmar Bergman	Not Available	Not Available	Bibi Andersson,Liv Ullmann,Margaretha Krook,Gu...	Ingmar Bergman	Ingmar Bergman	7	7
245	246	The Help	2011	8.1	Drama	PG-13	2h 26m	Change begins with a whisper.	25000000	216639112	Viola Davis,Emma Stone,Octavia Spencer,Bryce D...	Tate Taylor	Tate Taylor,Kathryn Stockett	8	8
246	247	Dersu Uzala	1975	8.2	Adventure,Biography,Drama	G	2h 22m	There is man and beast at nature's mercy. Ther...	4000000	14480	Maksim Munzuk,Yuriy Solomin,Mikhail Bychkov,Vl...	Akira Kurosawa	Akira Kurosawa,Yuriy Nagibin,Vladimir Arsenev	11	11
248	249	Gandhi	1982	8.0	Biography,Drama,History	PG	3h 11m	His Triumph Changed The World Forever.	22000000	52767889	Ben Kingsley,John Gielgud,Rohini Hattangadi,Ro...	Richard Attenborough	John Briley	6	6
249	250	Dances with Wolves	1990	8.0	Adventure,Drama,Western	PG-13	3h 1m	Inside everyone is a frontier waiting to be di...	22000000	424208848	Kevin Costner,Mary McDonnell,Graham Greene,Rod...	Kevin Costner	Michael Blake	18	18
177 rows × 15 columns
  1. Calculate how much years have passed since the release of each movie.

Solution
df['year']=df['year'].astype(int)
df['years_passed']=2025-df['year']
df

# Output
rank	name	year	rating	genre	certificate	run_time	tagline	budget	box_office	casts	directors	writers	title_length	years_passed
0	1	The Shawshank Redemption	1994	9.3	Drama	R	2h 22m	Fear can hold you prisoner. Hope can set you f...	25000000	28884504	Tim Robbins,Morgan Freeman,Bob Gunton,William ...	Frank Darabont	Stephen King,Frank Darabont	24	31
1	2	The Godfather	1972	9.2	Crime,Drama	R	2h 55m	An offer you can't refuse.	6000000	250341816	Marlon Brando,Al Pacino,James Caan,Diane Keato...	Francis Ford Coppola	Mario Puzo,Francis Ford Coppola	13	53
...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...
248	249	Gandhi	1982	8.0	Biography,Drama,History	PG	3h 11m	His Triumph Changed The World Forever.	22000000	52767889	Ben Kingsley,John Gielgud,Rohini Hattangadi,Ro...	Richard Attenborough	John Briley	6	43
249	250	Dances with Wolves	1990	8.0	Adventure,Drama,Western	PG-13	3h 1m	Inside everyone is a frontier waiting to be di...	22000000	424208848	Kevin Costner,Mary McDonnell,Graham Greene,Rod...	Kevin Costner	Michael Blake	18	35
250 rows × 15 columns

  1. Find out the oldest movie in the dataset.

Solution
ndf=df.loc[max(df['years_passed'])]
ndf

# Output
rank                                                          105
name                                            Full Metal Jacket
year                                                         1987
rating                                                        8.3
genre                                                   Drama,War
certificate                                                     R
run_time                                                   1h 56m
tagline         An Epic Story of the Vietnam War [Australia Th...
budget                                                   30000000
box_office                                               46358827
casts           Matthew Modine,R. Lee Ermey,Vincent D'Onofrio,...
directors                                         Stanley Kubrick
writers               Stanley Kubrick,Michael Herr,Gustav Hasford
title_length                                                   17
years_passed                                                   38
Name: 104, dtype: object
  1. Find out the number of writers in each movie.

Solution
df['writers_count']=df['writers'].str.split(',').str.len()
df

# Output
        rank	name	year	rating	genre	certificate	run_time	tagline	budget	box_office	casts	directors	writers	title_length	years_passed	writers_count
0	1	The Shawshank Redemption	1994	9.3	Drama	R	2h 22m	Fear can hold you prisoner. Hope can set you f...	25000000	28884504	Tim Robbins,Morgan Freeman,Bob Gunton,William ...	Frank Darabont	Stephen King,Frank Darabont	24	31	2
1	2	The Godfather	1972	9.2	Crime,Drama	R	2h 55m	An offer you can't refuse.	6000000	250341816	Marlon Brando,Al Pacino,James Caan,Diane Keato...	Francis Ford Coppola	Mario Puzo,Francis Ford Coppola	13	53	2
2	3	The Dark Knight	2008	9.0	Action,Crime,Drama	PG-13	2h 32m	Why So Serious?	185000000	1006234167	Christian Bale,Heath Ledger,Aaron Eckhart,Mich...	Christopher Nolan	Jonathan Nolan,Christopher Nolan,David S. Goyer	15	17	3
3	4	The Godfather Part II	1974	9.0	Crime,Drama	R	3h 22m	All the power on earth can't change destiny.	13000000	47961919	Al Pacino,Robert De Niro,Robert Duvall,Diane K...	Francis Ford Coppola	Francis Ford Coppola,Mario Puzo	21	51	2
4	5	12 Angry Men	1957	9.0	Crime,Drama	Approved	1h 36m	Life Is In Their Hands -- Death Is On Their Mi...	350000	955	Henry Fonda,Lee J. Cobb,Martin Balsam,John Fie...	Sidney Lumet	Reginald Rose	12	68	1
...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...
245	246	The Help	2011	8.1	Drama	PG-13	2h 26m	Change begins with a whisper.	25000000	216639112	Viola Davis,Emma Stone,Octavia Spencer,Bryce D...	Tate Taylor	Tate Taylor,Kathryn Stockett	8	14	2
246	247	Dersu Uzala	1975	8.2	Adventure,Biography,Drama	G	2h 22m	There is man and beast at nature's mercy. Ther...	4000000	14480	Maksim Munzuk,Yuriy Solomin,Mikhail Bychkov,Vl...	Akira Kurosawa	Akira Kurosawa,Yuriy Nagibin,Vladimir Arsenev	11	50	3
247	248	Aladdin	1992	8.0	Animation,Adventure,Comedy	G	1h 30m	Wish granted! (DVD re-release)	Not Available	Not Available	Scott Weinger,Robin Williams,Linda Larkin,Jona...	Ron Clements,John Musker	Ron Clements,John Musker,Ted Elliott	7	33	3
248	249	Gandhi	1982	8.0	Biography,Drama,History	PG	3h 11m	His Triumph Changed The World Forever.	22000000	52767889	Ben Kingsley,John Gielgud,Rohini Hattangadi,Ro...	Richard Attenborough	John Briley	6	43	1
249	250	Dances with Wolves	1990	8.0	Adventure,Drama,Western	PG-13	3h 1m	Inside everyone is a frontier waiting to be di...	22000000	424208848	Kevin Costner,Mary McDonnell,Graham Greene,Rod...	Kevin Costner	Michael Blake	18	35	1
250 rows × 16 columns
  1. Display the data where the movie titles satrt with 'The'.

Solution
ndf=df.loc[df['name'].str.startswith('The')]
ndf

# Output
rank	name	year	rating	genre	certificate	run_time	tagline	budget	box_office	casts	directors	writers	title_length	years_passed	writers_count
0	1	The Shawshank Redemption	1994	9.3	Drama	R	2h 22m	Fear can hold you prisoner. Hope can set you f...	25000000	28884504	Tim Robbins,Morgan Freeman,Bob Gunton,William ...	Frank Darabont	Stephen King,Frank Darabont	24	31	2
1	2	The Godfather	1972	9.2	Crime,Drama	R	2h 55m	An offer you can't refuse.	6000000	250341816	Marlon Brando,Al Pacino,James Caan,Diane Keato...	Francis Ford Coppola	Mario Puzo,Francis Ford Coppola	13	53	2
......................................................................
244	245	The Iron Giant	1999	8.1	Animation,Action,Adventure	PG	1h 26m	Some secrets are too huge to hide	70000000	23335817	Eli Marienthal,Harry Connick Jr.,Jennifer Anis...	Brad Bird	Tim McCanlies,Brad Bird,Ted Hughes	14	26	3
245	246	The Help	2011	8.1	Drama	PG-13	2h 26m	Change begins with a whisper.	25000000	216639112	Viola Davis,Emma Stone,Octavia Spencer,Bryce D...	Tate Taylor	Tate Taylor,Kathryn Stockett	8	14	2
  1. Display the movies whose taglines end with a ? or !

Solution
ndf=df.loc[ df['tagline'].str.endswith('!') | df['tagline'].str.endswith('?') ]
ndf

# Output
rank	name	year	rating	genre	certificate	run_time	tagline	budget	box_office	casts	directors	writers	title_length	years_passed	writers_count
4	5	12 Angry Men	1957	9.0	Crime,Drama	Approved	1h 36m	Life Is In Their Hands -- Death Is On Their Mi...	350000	955	Henry Fonda,Lee J. Cobb,Martin Balsam,John Fie...	Sidney Lumet	Reginald Rose	12	68	1
7	8	Pulp Fiction	1994	8.9	Crime,Drama	R	2h 34m	Girls like me don't make invitations like this...	8000000	213928762	John Travolta,Uma Thurman,Samuel L. Jackson,Br...	Quentin Tarantino	Quentin Tarantino,Roger Avary	12	31	2
19	20	Seven Samurai	1954	8.6	Action,Drama	Not Rated	3h 27m	Will Take Its Place With the Seven Greatest Fi...	125000000	346258	Toshirô Mifune,Takashi Shimura,Keiko Tsushima,...	Akira Kurosawa	Akira Kurosawa,Shinobu Hashimoto,Hideo Oguni	13	71	3
......................................................
232	233	The Grapes of Wrath	1940	8.1	Drama	Passed	2h 9m	The thousands who have read the book will know...	800000	800000 (estimated)	Henry Fonda,Jane Darwell,John Carradine,Charle...	John Ford	Nunnally Johnson,John Steinbeck	19	85	2
240	241	The Sound of Music	1965	8.1	Biography,Drama,Family	G	2h 52m	RADIANCE THAT FLOODS THE SCREEN...AND WARMS TH...	8200000	159428329	Julie Andrews,Christopher Plummer,Eleanor Park...	Robert Wise	Georg Hurdalek,Howard Lindsay,Russel Crouse	18	60	3
241	242	It Happened One Night	1934	8.1	Comedy,Romance	Passed	1h 45m	Two great lovers of the screen in the grandest...	325000	11477	Clark Gable,Claudette Colbert,Walter Connolly,...	Frank Capra	Robert Riskin,Samuel Hopkins Adams	21	91	2
  1. Distribute all the genres into different columns.

Solution
df[['genre_1','genre_2','genre_3']]=df['genre'].str.split(',',expand=True)
df

# Output

    rank	name	year	rating	genre	certificate	run_time	tagline	budget	box_office	casts	directors	writers	title_length	years_passed	writers_count	genre_1	genre_2	genre_3
0	1	The Shawshank Redemption	1994	9.3	Drama	R	2h 22m	Fear can hold you prisoner. Hope can set you f...	25000000	28884504	Tim Robbins,Morgan Freeman,Bob Gunton,William ...	Frank Darabont	Stephen King,Frank Darabont	24	31	2	Drama	None	None
1	2	The Godfather	1972	9.2	Crime,Drama	R	2h 55m	An offer you can't refuse.	6000000	250341816	Marlon Brando,Al Pacino,James Caan,Diane Keato...	Francis Ford Coppola	Mario Puzo,Francis Ford Coppola	13	53	2	Crime	Drama	None
2	3	The Dark Knight	2008	9.0	Action,Crime,Drama	PG-13	2h 32m	Why So Serious?	185000000	1006234167	Christian Bale,Heath Ledger,Aaron Eckhart,Mich...	Christopher Nolan	Jonathan Nolan,Christopher Nolan,David S. Goyer	15	17	3	Action	Crime	Drama
..................................................................................
248	249	Gandhi	1982	8.0	Biography,Drama,History	PG	3h 11m	His Triumph Changed The World Forever.	22000000	52767889	Ben Kingsley,John Gielgud,Rohini Hattangadi,Ro...	Richard Attenborough	John Briley	6	43	1	Biography	Drama	History
249	250	Dances with Wolves	1990	8.0	Adventure,Drama,Western	PG-13	3h 1m	Inside everyone is a frontier waiting to be di...	22000000	424208848	Kevin Costner,Mary McDonnell,Graham Greene,Rod...	Kevin Costner	Michael Blake	18	35	1	Adventure	Drama	Western
250 rows × 19 columns
  1. Create a new column that combines movie name and year.

Solution
df['name_year'] = df['name'] + ' (' + df['year'].astype(str) + ')'
df

# Output
ank	name	year	rating	genre	certificate	run_time	tagline	budget	box_office	casts	directors	writers	title_length	years_passed	writers_count	genre_1	genre_2	genre_3	name_year
0	1	The Shawshank Redemption	1994	9.3	Drama	R	2h 22m	Fear can hold you prisoner. Hope can set you f...	25000000	28884504	Tim Robbins,Morgan Freeman,Bob Gunton,William ...	Frank Darabont	Stephen King,Frank Darabont	24	31	2	Drama	None	None	The Shawshank Redemption (1994)
1	2	The Godfather	1972	9.2	Crime,Drama	R	2h 55m	An offer you can't refuse.	6000000	250341816	Marlon Brando,Al Pacino,James Caan,Diane Keato...	Francis Ford Coppola	Mario Puzo,Francis Ford Coppola	13	53	2	Crime	Drama	None	The Godfather (1972)
......................................................................................
248	249	Gandhi	1982	8.0	Biography,Drama,History	PG	3h 11m	His Triumph Changed The World Forever.	22000000	52767889	Ben Kingsley,John Gielgud,Rohini Hattangadi,Ro...	Richard Attenborough	John Briley	6	43	1	Biography	Drama	History	Gandhi (1982)
249	250	Dances with Wolves	1990	8.0	Adventure,Drama,Western	PG-13	3h 1m	Inside everyone is a frontier waiting to be di...	22000000	424208848	Kevin Costner,Mary McDonnell,Graham Greene,Rod...	Kevin Costner	Michael Blake	18	35	1	Adventure	Drama	Western	Dances with Wolves (1990)
250 rows × 20 columns

109KB
IMDB Top 250 Movies.csv