Using inplace parameter in pandas
Using "inplace" parameter

This introduction to pandas is derived from Data School's pandas Q&A with my own notes and code.

Using "inplace" parameter in pandas

In [1]:
import pandas as pd
In [2]:
url = 'http://bit.ly/uforeports'
ufo = pd.read_csv(url)
In [3]:
ufo.shape
Out[3]:
(18241, 5)
In [4]:
ufo.head()
Out[4]:
City Colors Reported Shape Reported State Time
0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00
1 Willingboro NaN OTHER NJ 6/30/1930 20:00
2 Holyoke NaN OVAL CO 2/15/1931 14:00
3 Abilene NaN DISK KS 6/1/1931 13:00
4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00
In [5]:
# dropping City column
ufo.drop('City', axis=1).head()
Out[5]:
Colors Reported Shape Reported State Time
0 NaN TRIANGLE NY 6/1/1930 22:00
1 NaN OTHER NJ 6/30/1930 20:00
2 NaN OVAL CO 2/15/1931 14:00
3 NaN DISK KS 6/1/1931 13:00
4 NaN LIGHT NY 4/18/1933 19:00
In [7]:
# you can see that the City column is not gone 
# drop() method has inplace=False as default
ufo.head()
Out[7]:
City Colors Reported Shape Reported State Time
0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00
1 Willingboro NaN OTHER NJ 6/30/1930 20:00
2 Holyoke NaN OVAL CO 2/15/1931 14:00
3 Abilene NaN DISK KS 6/1/1931 13:00
4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00
In [8]:
# you want to change to inplace=True to affect the underlying data
ufo.drop('City', axis=1, inplace=True)
In [9]:
ufo.head()
Out[9]:
Colors Reported Shape Reported State Time
0 NaN TRIANGLE NY 6/1/1930 22:00
1 NaN OTHER NJ 6/30/1930 20:00
2 NaN OVAL CO 2/15/1931 14:00
3 NaN DISK KS 6/1/1931 13:00
4 NaN LIGHT NY 4/18/1933 19:00
In [11]:
# dropna with how='any' would drop any row with 'NaN'
ufo.dropna(how='any').shape
Out[11]:
(2490, 4)
In [14]:
ufo.shape
# as you can see, we lose a lot of rows because of dropna
# but the underlying data has not been affected because inplace=False for .dropna()
Out[14]:
(18241, 4)
In [15]:
# some examples with inplace=False
# most are set to False

# ufo.set_index()
# ufo.rename()
In [16]:
# you can not use inplace=True and use an assignment instead
ufo = ufo.set_index('Time')
In [17]:
ufo.tail()
Out[17]:
Colors Reported Shape Reported State
Time
12/31/2000 23:00 NaN TRIANGLE IL
12/31/2000 23:00 NaN DISK IA
12/31/2000 23:45 NaN NaN WI
12/31/2000 23:45 RED LIGHT WI
12/31/2000 23:59 NaN OVAL FL
In [18]:
ufo.fillna(method='bfill').tail()
Out[18]:
Colors Reported Shape Reported State
Time
12/31/2000 23:00 RED TRIANGLE IL
12/31/2000 23:00 RED DISK IA
12/31/2000 23:45 RED LIGHT WI
12/31/2000 23:45 RED LIGHT WI
12/31/2000 23:59 NaN OVAL FL
In [19]:
ufo.fillna(method='ffill').tail()
Out[19]:
Colors Reported Shape Reported State
Time
12/31/2000 23:00 RED TRIANGLE IL
12/31/2000 23:00 RED DISK IA
12/31/2000 23:45 RED DISK WI
12/31/2000 23:45 RED LIGHT WI
12/31/2000 23:59 RED OVAL FL
Tags: pandas