Python for Data Analysis Lightning Tutorials is a series of tutorials in Data Analysis, Statistics, and Graphics using Python. The Pandas Cookbook series of tutorials provides recipes for common tasks and moves on to more advanced topics in statistics and time series analysis.
Created by Alfred Essa, Apr 27th, 2014
Note: IPython Notebook and Data files can be found at my Github Site: http://github/alfredessa
In this tutorial we learn to apply Python slicing operations to a Pandas DataFrame object.
In [1]:
# load pandas and numpy
import pandas as pd
import numpy as np
In [2]:
# ipython magic for inline plots
%pylab inline
In [3]:
# we want to a create a dataframe that has dates as rows and cities as columns,
# the cells will be temperatures
# first let's define a date range, the first two months of 2014
days = pd.date_range('2014-01-01', '2014-02-28', freq = 'D')
In [ ]:
# then we will create a tuple defining the dimensions of our table (59 x 5)
In [4]:
dim = (59,5)
In [5]:
# define the data frame
df = pd.DataFrame(np.random.random_integers(-20,40,dim), index=days, columns=['Madrid','Boston','Tokyo','Shanghai','Kolkata'])
In [7]:
# check the values
df.tail()
Out[7]:
In [8]:
# range of rows and range of columns
df.ix[3:6, 'Madrid': 'Tokyo']
Out[8]:
In [9]:
# specific rows and specific columns
df.ix[[3,20,49], ['Boston', 'Shanghai']]
Out[9]:
In [10]:
# specific rows and all columns
df.ix[[3,9,11,13], :]
Out[10]:
In [11]:
# all rows and range of columns
df.ix[ :, 'Madrid': 'Tokyo']
Out[11]:
In [12]:
# range of rows and specific columns
df.ix[3:11, ['Boston','Shanghai']]
Out[12]:
In [13]:
m = pd.read_csv('data/mortality.csv')
In [15]:
In [17]:
m = m.set_index('Country Name')
In [18]:
m.head()
Out[18]:
In [19]:
t = m.T
In [20]:
t.head()
Out[20]:
In [21]:
comparison = t[['Bangladesh','India','Rwanda','Uganda']]
In [22]:
comparison.tail()
Out[22]:
In [23]:
comparison.plot()
Out[23]:
In [ ]: