Last Updated: 02-April-2016

Here's my intro to data slicing with Python Pandas (using Jupyter)...

In [1]:
import pandas as pd

# create (completely random!) dataframe
df = pd.DataFrame({'name':['craig', 'michael', 'steve'], 'age':[30, 31, 25], 'pet':['dog', 'cat', 'hamster']})
In [2]:
# ok, so we've created the dataframe simply using a dictionary... let's take a look...
df
Out[2]:
  age name pet
0 30 craig dog
1 31 michael cat
2 25 steve hamster
In [3]:
# we can see the table is printed, along with an index in the far left column
In [4]:
# to access a column, we can simple use a dot notation, as follows...
df.name
Out[4]:
0      craig
1    michael
2      steve
Name: name, dtype: object
In [5]:
# again, this displays the column along with the index
In [6]:
# we can also access a column using the following...
df['name']
Out[6]:
0      craig
1    michael
2      steve
Name: name, dtype: object
In [7]:
# we can use ilo[] to specify the rows and columns...
df.iloc[:, 1] # .iloc[row, column] 
Out[7]:
0      craig
1    michael
2      steve
Name: name, dtype: object
In [8]:
# or...gets the second row
df.iloc[1, :]
Out[8]:
age          31
name    michael
pet         cat
Name: 1, dtype: object
In [9]:
# multiple columns...
df.iloc[:, 1:3]
Out[9]:
  name pet
0 craig dog
1 michael cat
2 steve hamster
In [10]:
# we can use .loc[row, column] to specify an index (if we know it)
df.loc[1]
Out[10]:
age          31
name    michael
pet         cat
Name: 1, dtype: object
In [11]:
# we can see it's produced the second row
In [12]:
# we can specify the a row and column
df.loc[0, 'pet'] # i.e. index or row 1 and the 'pet' column
Out[12]:
'dog'

So that's it, fairly simple but if you're new to Pandas, it will hopefully be helpful for you.

About the author

Image

Craig Addyman @craigaddyman
Head of Digital Marketing. Python Coder.