Removing columns from a pandas DataFrame
This introduction to pandas is derived from Data School's pandas Q&A with my own notes and code.
Removing columns from a pandas DataFrame¶
In [1]:
import pandas as pd
In [2]:
# Creating pandas DataFrame
url = 'http://bit.ly/uforeports'
ufo = pd.read_csv(url)
In [3]:
ufo.head()
Out[3]:
In [4]:
ufo.shape
Out[4]:
In [5]:
# Removing column
# axis=0 row axis
# axis=1 column axis
# inplace=True to effect change
ufo.drop('Colors Reported', axis=1, inplace=True)
In [6]:
ufo.head()
Out[6]:
In [7]:
# Removing column
list_drop = ['City', 'State']
ufo.drop(list_drop, axis=1, inplace=True)
In [9]:
ufo.head()
Out[9]:
In [10]:
# Removing rows 0 and 1
# axis=0 is the default, so technically, you can leave this out
rows = [0, 1]
ufo.drop(rows, axis=0, inplace=True)
In [12]:
ufo.head()
Out[12]: