Profile picture

Pandas Immutability and the Inplace Parameter

Last updated: May 14th, 20192019-05-14Project preview

rmotr


Pandas Immutability and the inplace parameter

In this lecture, we'll learn how to modify a DataFrame using the inplace parameter and see how it impacts a method's execution end result.

We'll also try methods with and without inplace parameter to demonstrate its effect.

purple-divider

Hands on!

In [30]:
import numpy as np
import pandas as pd

green-divider

We'll start by reading a movies DataFrame:

In [32]:
movies = pd.read_csv('data/movies.csv')
del movies['Unnamed: 0']

movies.head()
Out[32]:
director_name duration genres movie_title title_year imdb_score
0 James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi Avatar 2009.0 7.9
1 Gore Verbinski 169.0 Action|Adventure|Fantasy Pirates of the Caribbean: At World's End NaN 7.1
2 NaN 148.0 Action|Adventure|Thriller Spectre 2015.0 6.8
3 NaN 164.0 Action|Thriller The Dark Knight Rises 2012.0 8.5
4 Doug Walker NaN Documentary Star Wars: Episode VII - The Force Awakens  ... NaN 7.1
In [33]:
movies.shape
Out[33]:
(5043, 6)

green-divider

set_index

Without inplace

In [34]:
movies.set_index('movie_title').head()
Out[34]:
director_name duration genres title_year imdb_score
movie_title
Avatar James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
Pirates of the Caribbean: At World's End Gore Verbinski 169.0 Action|Adventure|Fantasy NaN 7.1
Spectre NaN 148.0 Action|Adventure|Thriller 2015.0 6.8
The Dark Knight Rises NaN 164.0 Action|Thriller 2012.0 8.5
Star Wars: Episode VII - The Force Awakens Doug Walker NaN Documentary NaN 7.1
In [35]:
movies.head()
Out[35]:
director_name duration genres movie_title title_year imdb_score
0 James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi Avatar 2009.0 7.9
1 Gore Verbinski 169.0 Action|Adventure|Fantasy Pirates of the Caribbean: At World's End NaN 7.1
2 NaN 148.0 Action|Adventure|Thriller Spectre 2015.0 6.8
3 NaN 164.0 Action|Thriller The Dark Knight Rises 2012.0 8.5
4 Doug Walker NaN Documentary Star Wars: Episode VII - The Force Awakens  ... NaN 7.1

 With inplace

In [36]:
movies.set_index('movie_title', inplace=True)
In [37]:
movies.head()
Out[37]:
director_name duration genres title_year imdb_score
movie_title
Avatar James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
Pirates of the Caribbean: At World's End Gore Verbinski 169.0 Action|Adventure|Fantasy NaN 7.1
Spectre NaN 148.0 Action|Adventure|Thriller 2015.0 6.8
The Dark Knight Rises NaN 164.0 Action|Thriller 2012.0 8.5
Star Wars: Episode VII - The Force Awakens Doug Walker NaN Documentary NaN 7.1

green-divider

reset_index

Without inplace

In [40]:
movies.reset_index().head()
Out[40]:
movie_title director_name duration genres title_year imdb_score
0 Avatar James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
1 Pirates of the Caribbean: At World's End Gore Verbinski 169.0 Action|Adventure|Fantasy NaN 7.1
2 Spectre NaN 148.0 Action|Adventure|Thriller 2015.0 6.8
3 The Dark Knight Rises NaN 164.0 Action|Thriller 2012.0 8.5
4 Star Wars: Episode VII - The Force Awakens  ... Doug Walker NaN Documentary NaN 7.1
In [41]:
movies.head()
Out[41]:
director_name duration genres title_year imdb_score
movie_title
Avatar James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
Pirates of the Caribbean: At World's End Gore Verbinski 169.0 Action|Adventure|Fantasy NaN 7.1
Spectre NaN 148.0 Action|Adventure|Thriller 2015.0 6.8
The Dark Knight Rises NaN 164.0 Action|Thriller 2012.0 8.5
Star Wars: Episode VII - The Force Awakens Doug Walker NaN Documentary NaN 7.1

 With inplace

In [42]:
movies.reset_index(inplace=True)
In [43]:
movies.head()
Out[43]:
movie_title director_name duration genres title_year imdb_score
0 Avatar James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
1 Pirates of the Caribbean: At World's End Gore Verbinski 169.0 Action|Adventure|Fantasy NaN 7.1
2 Spectre NaN 148.0 Action|Adventure|Thriller 2015.0 6.8
3 The Dark Knight Rises NaN 164.0 Action|Thriller 2012.0 8.5
4 Star Wars: Episode VII - The Force Awakens  ... Doug Walker NaN Documentary NaN 7.1

green-divider

rename

Without inplace

In [44]:
movies.rename(columns={
    'title_year': 'year'
}).head()
Out[44]:
movie_title director_name duration genres year imdb_score
0 Avatar James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
1 Pirates of the Caribbean: At World's End Gore Verbinski 169.0 Action|Adventure|Fantasy NaN 7.1
2 Spectre NaN 148.0 Action|Adventure|Thriller 2015.0 6.8
3 The Dark Knight Rises NaN 164.0 Action|Thriller 2012.0 8.5
4 Star Wars: Episode VII - The Force Awakens  ... Doug Walker NaN Documentary NaN 7.1
In [45]:
movies.head()
Out[45]:
movie_title director_name duration genres title_year imdb_score
0 Avatar James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
1 Pirates of the Caribbean: At World's End Gore Verbinski 169.0 Action|Adventure|Fantasy NaN 7.1
2 Spectre NaN 148.0 Action|Adventure|Thriller 2015.0 6.8
3 The Dark Knight Rises NaN 164.0 Action|Thriller 2012.0 8.5
4 Star Wars: Episode VII - The Force Awakens  ... Doug Walker NaN Documentary NaN 7.1

With inplace

In [46]:
movies.rename(columns={
    'title_year': 'year'
}, inplace=True)
In [47]:
movies.head()
Out[47]:
movie_title director_name duration genres year imdb_score
0 Avatar James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
1 Pirates of the Caribbean: At World's End Gore Verbinski 169.0 Action|Adventure|Fantasy NaN 7.1
2 Spectre NaN 148.0 Action|Adventure|Thriller 2015.0 6.8
3 The Dark Knight Rises NaN 164.0 Action|Thriller 2012.0 8.5
4 Star Wars: Episode VII - The Force Awakens  ... Doug Walker NaN Documentary NaN 7.1

green-divider

drop

Without inplace

In [54]:
# dropping columns
movies.drop('year', axis=1).head()
Out[54]:
movie_title director_name duration genres imdb_score
0 Avatar James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi 7.9
5 John Carter Andrew Stanton 132.0 Action|Adventure|Sci-Fi 6.6
7 Tangled Nathan Greno 100.0 Adventure|Animation|Comedy|Family|Fantasy|Musi... 7.8
8 Avengers: Age of Ultron Joss Whedon 141.0 Action|Adventure|Sci-Fi 7.5
11 Superman Returns Bryan Singer 169.0 Action|Adventure|Sci-Fi 6.1
In [56]:
# dropping rows
movies.drop([0, 8]).head()
Out[56]:
movie_title director_name duration genres year imdb_score
5 John Carter Andrew Stanton 132.0 Action|Adventure|Sci-Fi 2012.0 6.6
7 Tangled Nathan Greno 100.0 Adventure|Animation|Comedy|Family|Fantasy|Musi... 2010.0 7.8
11 Superman Returns Bryan Singer 169.0 Action|Adventure|Sci-Fi 2006.0 6.1
12 Quantum of Solace Marc Forster 106.0 Action|Adventure 2008.0 6.7
14 The Lone Ranger Gore Verbinski 150.0 Action|Adventure|Western 2013.0 6.5
In [57]:
movies.head()
Out[57]:
movie_title director_name duration genres year imdb_score
0 Avatar James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
5 John Carter Andrew Stanton 132.0 Action|Adventure|Sci-Fi 2012.0 6.6
7 Tangled Nathan Greno 100.0 Adventure|Animation|Comedy|Family|Fantasy|Musi... 2010.0 7.8
8 Avengers: Age of Ultron Joss Whedon 141.0 Action|Adventure|Sci-Fi 2015.0 7.5
11 Superman Returns Bryan Singer 169.0 Action|Adventure|Sci-Fi 2006.0 6.1

With inplace

In [58]:
movies.drop('year', axis=1, inplace=True)
movies.drop([0, 8], inplace=True)
In [59]:
movies.head()
Out[59]:
movie_title director_name duration genres imdb_score
5 John Carter Andrew Stanton 132.0 Action|Adventure|Sci-Fi 6.6
7 Tangled Nathan Greno 100.0 Adventure|Animation|Comedy|Family|Fantasy|Musi... 7.8
11 Superman Returns Bryan Singer 169.0 Action|Adventure|Sci-Fi 6.1
12 Quantum of Solace Marc Forster 106.0 Action|Adventure 6.7
14 The Lone Ranger Gore Verbinski 150.0 Action|Adventure|Western 6.5

green-divider

dropna

Without inplace

In [48]:
movies.dropna(how='all').head()
Out[48]:
movie_title director_name duration genres year imdb_score
0 Avatar James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
5 John Carter Andrew Stanton 132.0 Action|Adventure|Sci-Fi 2012.0 6.6
7 Tangled Nathan Greno 100.0 Adventure|Animation|Comedy|Family|Fantasy|Musi... 2010.0 7.8
8 Avengers: Age of Ultron Joss Whedon 141.0 Action|Adventure|Sci-Fi 2015.0 7.5
11 Superman Returns Bryan Singer 169.0 Action|Adventure|Sci-Fi 2006.0 6.1
In [49]:
movies.head()
Out[49]:
movie_title director_name duration genres year imdb_score
0 Avatar James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
1 Pirates of the Caribbean: At World's End Gore Verbinski 169.0 Action|Adventure|Fantasy NaN 7.1
2 Spectre NaN 148.0 Action|Adventure|Thriller 2015.0 6.8
3 The Dark Knight Rises NaN 164.0 Action|Thriller 2012.0 8.5
4 Star Wars: Episode VII - The Force Awakens  ... Doug Walker NaN Documentary NaN 7.1

With inplace

In [50]:
movies.dropna(how='all', inplace=True)
In [51]:
movies.head()
Out[51]:
movie_title director_name duration genres year imdb_score
0 Avatar James Cameron 178.0 Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
5 John Carter Andrew Stanton 132.0 Action|Adventure|Sci-Fi 2012.0 6.6
7 Tangled Nathan Greno 100.0 Adventure|Animation|Comedy|Family|Fantasy|Musi... 2010.0 7.8
8 Avengers: Age of Ultron Joss Whedon 141.0 Action|Adventure|Sci-Fi 2015.0 7.5
11 Superman Returns Bryan Singer 169.0 Action|Adventure|Sci-Fi 2006.0 6.1

green-divider

sort_values

Without inplace

In [62]:
movies.sort_values(by=['movie_title']).head()
Out[62]:
movie_title director_name duration genres imdb_score
4447 #Horror Tara Subkoff 101.0 Drama|Horror|Mystery|Thriller 3.3
3698 10 Cloverfield Lane Dan Trachtenberg 104.0 Drama|Horror|Mystery|Sci-Fi|Thriller 7.3
3015 10 Days in a Madhouse Timothy Hines 111.0 Drama 7.5
2845 10 Things I Hate About You Gil Junger 97.0 Comedy|Drama|Romance 7.2
406 102 Dalmatians Kevin Lima 100.0 Adventure|Comedy|Family 4.8
In [63]:
movies.head()
Out[63]:
movie_title director_name duration genres imdb_score
5 John Carter Andrew Stanton 132.0 Action|Adventure|Sci-Fi 6.6
7 Tangled Nathan Greno 100.0 Adventure|Animation|Comedy|Family|Fantasy|Musi... 7.8
11 Superman Returns Bryan Singer 169.0 Action|Adventure|Sci-Fi 6.1
12 Quantum of Solace Marc Forster 106.0 Action|Adventure 6.7
14 The Lone Ranger Gore Verbinski 150.0 Action|Adventure|Western 6.5

With inplace

In [64]:
movies.sort_values(by=['movie_title'], inplace=True)
In [65]:
movies.head()
Out[65]:
movie_title director_name duration genres imdb_score
4447 #Horror Tara Subkoff 101.0 Drama|Horror|Mystery|Thriller 3.3
3698 10 Cloverfield Lane Dan Trachtenberg 104.0 Drama|Horror|Mystery|Sci-Fi|Thriller 7.3
3015 10 Days in a Madhouse Timothy Hines 111.0 Drama 7.5
2845 10 Things I Hate About You Gil Junger 97.0 Comedy|Drama|Romance 7.2
406 102 Dalmatians Kevin Lima 100.0 Adventure|Comedy|Family 4.8

purple-divider

Notebooks AI
Notebooks AI Profile20060