Profile picture

Pandas Immutability and the Inplace Parameter

Last updated: November 29th, 20192019-11-29Project 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 [1]:
import numpy as np
import pandas as pd

green-divider

We'll start by reading a movies DataFrame:

In [2]:
movies = pd.read_csv('movies.csv')

movies.head()
Out[2]:
director_name duration genres movie_title title_year imdb_score
0 James Cameron NaN Action|Adventure|Fantasy|Sci-Fi Avatar 2009.0 7.9
1 Gore Verbinski 169.0 Action|Adventure|Fantasy NaN 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 [3]:
movies.shape
Out[3]:
(5043, 6)
In [5]:
movies.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5043 entries, 0 to 5042
Data columns (total 6 columns):
director_name    4931 non-null object
duration         5027 non-null float64
genres           5042 non-null object
movie_title      5037 non-null object
title_year       4932 non-null float64
imdb_score       5043 non-null float64
dtypes: float64(3), object(3)
memory usage: 236.5+ KB

green-divider

set_index

Without inplace

In [6]:
movies.set_index('movie_title').head()
Out[6]:
director_name duration genres title_year imdb_score
movie_title
Avatar James Cameron NaN Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
NaN 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 [7]:
movies.head()
Out[7]:
director_name duration genres movie_title title_year imdb_score
0 James Cameron NaN Action|Adventure|Fantasy|Sci-Fi Avatar 2009.0 7.9
1 Gore Verbinski 169.0 Action|Adventure|Fantasy NaN 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 [8]:
movies.set_index('movie_title', inplace=True)
In [9]:
movies.head()
Out[9]:
director_name duration genres title_year imdb_score
movie_title
Avatar James Cameron NaN Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
NaN 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 [10]:
movies.reset_index().head()
Out[10]:
movie_title director_name duration genres title_year imdb_score
0 Avatar James Cameron NaN Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
1 NaN 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 [11]:
movies.head()
Out[11]:
director_name duration genres title_year imdb_score
movie_title
Avatar James Cameron NaN Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
NaN 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 [12]:
movies.reset_index(inplace=True)
In [13]:
movies.head()
Out[13]:
movie_title director_name duration genres title_year imdb_score
0 Avatar James Cameron NaN Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
1 NaN 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 [14]:
movies.rename(columns={
    'title_year': 'year'
}).head()
Out[14]:
movie_title director_name duration genres year imdb_score
0 Avatar James Cameron NaN Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
1 NaN 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 [15]:
movies.head()
Out[15]:
movie_title director_name duration genres title_year imdb_score
0 Avatar James Cameron NaN Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
1 NaN 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 [16]:
movies.rename(columns={
    'title_year': 'year'
}, inplace=True)
In [ ]:
movies.head()

green-divider

drop

Without inplace

In [ ]:
# dropping columns
movies.drop('year', axis=1).head()
In [ ]:
# dropping rows
movies.drop([0, 8]).head()
In [ ]:
movies.head()

With inplace

In [ ]:
movies.drop('year', axis=1, inplace=True)
In [ ]:
movies.head()
In [ ]:
movies.drop([0, 8], inplace=True)
In [ ]:
movies.head()

green-divider

dropna

Without inplace

In [17]:
movies.dropna(how='all').head()
Out[17]:
movie_title director_name duration genres year imdb_score
0 Avatar James Cameron NaN Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
1 NaN 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 [18]:
movies.head()
Out[18]:
movie_title director_name duration genres year imdb_score
0 Avatar James Cameron NaN Action|Adventure|Fantasy|Sci-Fi 2009.0 7.9
1 NaN 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 [ ]:
movies.dropna(how='all', inplace=True)
In [ ]:
movies.head()

green-divider

sort_values

Without inplace

In [ ]:
movies.sort_values(by=['movie_title']).head()
In [ ]:
movies.head()

With inplace

In [ ]:
movies.sort_values(by=['movie_title'], inplace=True)
In [ ]:
movies.head()

purple-divider

Notebooks AI
Notebooks AI Profile20060