Professional Documents
Culture Documents
UNIT-1
Syllabus: Why Statistics?, Python Packages for Statistics, First Python Programs, Pandas: Data
Structures for Statistics, Data Input: Input from Text Files: Visual Inspection, Reading ASCII-
Data into Python, Input from MS Excel, Datatypes: Categorical, Numerical.
Definitions:
Statistics are the sets of mathematical equations that we used to analyze the available data.
It provides information.
The field of statistics is the science of learning from data
1. Why Statistics?: In general, statistics will help to
Clarify the question.
Identify the variable and the measure of that variable that will answer that question.
Determine the required sample size.
Describe variation.
Make quantitative statements about estimated parameters.
Make predictions based on your data.
Following are few of the examples that explain how statistics are helpful in our daily activities:
a. Weather forecasting: Whenever you are planning a trip or a journey, you take in
account the weather conditions of the place. Have you ever think how do you get that
information? There are some computers models build on statistical concepts. These
computer models compare prior weather with the current weather and predict future
weather.
b. Online shopping: Every time you go to an online shopping site like Flipkart or
Amazon, you see the ratings of the products, the reviews of the customers etc. All
this data that you see is available with the help of but statistics.
c. Politics: Every time you go to cast your vote, you compare the performance of the
current government to that of the other previous governments. You also decide whom to
vote on the basis of the promises made by the other party leaders which often use phrases
like “We will try to improve the education rates to ……….”. All this data is part of
statistics. News reporter makes a prediction of winner for elections based on political
campaigns.
d. Insurance: You know that in order to drive your car you are required by law to have
car insurance. If you have a mortgage on your house, you must have it insured as
well. The rate that an insurance company charges you is based upon statistics from
all drivers or homeowners in your area.
e. Stock market: Stock market is a concept we all hear about in our day to day lives
and it will not come as a surprise to you that it is completely based on statistics as
stock analysts also use statistical computer models to forecast what is happening in
the economy.
f. Sports: Sport selections are mostly done on the basis of data about the performance
and the form of the player. Also in the modern world that we live in statistics of
player make the match or game more interesting and provide ground for fans to
argue about the dominance of their favorite player in the game.
g. Medical: Statistics play a big role in the medical field. Before any drugs prescribed,
scientist must show a statistically valid rate of effectiveness. Statistics are behind all the
study of medical. Doctors predict disease based on statistics concepts. Suppose a survey
shows that 75%-80% people have cancer and not able to find the reason. When the
statistics become involved, then you can have a better idea of how the cancer may affect
your body or is smoking is the major reason for it.
To facilitate the use of Python, the so-called Python distributions collect matching versions of
the most important packages. Popular Python distributions are:
• WinPython recommended for Windows users. At the time of writing, the latest version was
3.5.1.3 (newer versions also ok). WinPython, which is free and customizable.
https://winpython.github.io/
• Anaconda by Continuum. For Windows, Mac, and Linux. Can be used to install Python 2.x
and 3.x, even simultaneously! The latest Anaconda version at time of writing was 4.0.0 (newer
versions also ok). Anaconda has become very popular recently, and is free for educational
purposes. https://store.continuum.io/cshop/anaconda/
Neither of these two distributions requires administrator rights.
Python 2.7.10 and 3.5.1, under Windows and Linux, using the following package versions:
ipython 4.1.2 : : : For interactive work.
numpy 1.11.0 : : : For working with vectors and arrays.
scipy 0.17.1 : : : All the essential scientific algorithms, including those for basic
statistics.
matplotlib 1.5.1 : : : The de-facto standard module for plotting and visualization.
pandas 0.18.0 : : : Adds DataFrames (imagine powerful spreadsheets) to Python.
patsy 0.4.1 : : : For working with statistical formulas.
statsmodels 0.8.0 : : : For statistical modeling and advanced analysis.
seaborn 0.7.0 : : : For visualization of statistical data.
In addition to these fairly general packages, some specialized packages have also
been used in the examples accompanying this book:
xlrd 0.9.4 : : : For reading and writing MS Excel files.
PyMC 2.3.6 : : : For Bayesian statistics, including Markov chain Monte Carlo
simulations.
scikit-learn 0.17.1 : : : For machine learning.
scikits.bootstrap 0.3.2 : : : Provides bootstrap confidence interval algorithms for
scipy.
lifelines 0.9.1.0 : : : Survival analysis in Python.
rpy2 2.7.4 : : : Provides a wrapper for R-functions in Python.
Most of these packages come either with the WinPython or Anaconda distributions, or can be
installed easily using pip or conda.
2.2. PyPI (The Python Package Index): It is a repository of software for the Python
programming language currently with more than 80,000 packages.
Packages from PyPI can be installed easily, from the Windows command shell (cmd) or the
Linux terminal, with:
$pip install [_package_]
To update a package, use:
$pip install [_package_] -U
To get a list of all the Python packages installed on your computer, type
$pip list
Note: Anaconda uses conda, a more powerful installation manager. But pip also works with
Anaconda.
2.3. Installation of Python
a. Under Windows:
Install Python:
Download WinPython from https://winpython.github.io/.
Run the downloaded .exe-file, and install WinPython into the [_WinPythonDir_] of your choice.
After the installation, make a change to your Windows Environment, by typing Win -> env -> Edit
environment variables for your account:
There is also the step value, which can be used with any of the above:
a[start:end:step] # start through not past end, by step
The key points to remember are that indexing starts at 0, not at 1; The other feature is that
start or end may be a negative number, which means it counts from the end of the array
instead of the beginning. So:
a[-1] # last item in the array
a[-2:] # last two items in the array
a[:-2] # everything except the last two items
As a result, a[:5] gives you the first five elements
10 23 56 17 52 61 73 90 26 72
Key Points:
Homogeneous data
Size Immutable
Values of Data Mutable
b. DataFrame: DataFrame is a two-dimensional array with heterogeneous data. For example,
1 Data - data takes various forms like ndarray, list, constants and dicts
2 Index - Index values must be unique and hashable, same length as data.
Default np.arrange(n) if no index is passed.
3 Dtype - dtype is for data type. If None, data type will be inferred
import pandas as pd
s = pd.Series()
print s
Its output is as follows −
Example 1
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data)
print s
Its output is as follows −
0 a
1 b
2 c
3 d
dtype: object
We did not pass any index, so by default, it assigned the indexes ranging from 0 to len(data)-1,
i.e., 0 to 3.
Example 2
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data,index=[100,101,102,103])
print s
Its output is as follows −
100 a
101 b
102 c
103 d
dtype: object
We passed the index values here. Now we can see the customized indexed values in the output.
Example 1
import pandas as pd
import numpy as np
s = pd.Series(data)
print s
Its output is as follows −
a 0.0
b 1.0
c 2.0
dtype: float64
Observe − Dictionary keys are used to construct index.
Example 2
import pandas as pd
import numpy as np
s = pd.Series(data,index=['b','c','d','a'])
print s
Its output is as follows −
b 1.0
c 2.0
d NaN
a 0.0
dtype: float64
Observe − Index order is persisted and the missing element is filled with NaN (Not a Number).
import pandas as pd
import numpy as np
print s
Its output is as follows −
0 5
1 5
2 5
3 5
dtype: int64
e. Accessing Data from Series with Position: Data in the series can be accessed similar to that in
an ndarray.
Example 1
Retrieve the first element. As we already know, the counting starts from zero for the array,
which means the first element is stored at zeroth position and so on.
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print s[0]
Its output is as follows −
Example 2
Retrieve the first three elements in the Series. If a : is inserted in front of it, all items from that
index onwards will be extracted. If two parameters (with : between them) is used, items
between the two indexes (not including the stop index)
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print s[:3]
Its output is as follows −
a 1
b 2
c 3
dtype: int64
Example 3
Retrieve the last three elements.
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print s[-3:]
Its output is as follows −
c 3
d 4
e 5
dtype: int64
Example 1
Retrieve a single element using index label value.
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print s['a']
Its output is as follows −
Example 2
Retrieve multiple elements using a list of index label values.
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print s[['a','c','d']]
Its output is as follows −
a 1
c 3
d 4
dtype: int64
Example 3
If a label is not contained, an exception is raised.
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print s['f']
Its output is as follows −
…
KeyError: 'f'
Pandas.DataFrames
A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in
rows and columns.
Features of DataFrame
pandas.DataFrame
A pandas DataFrame can be created using the following constructor −
1 data
data takes various forms like ndarray, series, map, lists, dict, constants and also
another DataFrame.
2 index
For the row labels, the Index to be used for the resulting frame is Optional Default
np.arrange(n) if no index is passed.
3 columns
For column labels, the optional default syntax is - np.arrange(n). This is only true
if no index is passed.
4 dtype
Data type of each column.
4 copy
This command (or whatever it is) is used for copying of data, if the default is
False.
Create DataFrame
A pandas DataFrame can be created using various inputs like −
Lists
dict
Series
Numpy ndarrays
Another DataFrame
In the subsequent sections of this chapter, we will see how to create a DataFrame using these
inputs.
Example
import pandas as pd
df = pd.DataFrame()
print df
Its output is as follows −
Empty DataFrame
Columns: []
Index: []
Example 1
import pandas as pd
data = [1,2,3,4,5]
df = pd.DataFrame(data)
print df
Its output is as follows −
0
0 1
1 2
2 3
3 4
4 5
Example 2
import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
print df
Its output is as follows −
Name Age
0 Alex 10
1 Bob 12
2 Clarke 13
Example 3
import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'],dtype=float)
print df
Its output is as follows −
Name Age
0 Alex 10.0
1 Bob 12.0
2 Clarke 13.0
Note − Observe, the dtype parameter changes the type of Age column to floating point.
If no index is passed, then by default, index will be range(n), where n is the array length.
Example 1
import pandas as pd
df = pd.DataFrame(data)
print df
Its output is as follows −
Age Name
0 28 Tom
1 34 Jack
2 29 Steve
3 42 Ricky
Note − Observe the values 0,1,2,3. They are the default index assigned to each using the
function range(n).
Example 2
Let us now create an indexed DataFrame using arrays.
import pandas as pd
df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])
print df
Its output is as follows −
Age Name
rank1 28 Tom
rank2 34 Jack
rank3 29 Steve
rank4 42 Ricky
Note − Observe, the index parameter assigns an index to each row.
Example 1
The following example shows how to create a DataFrame by passing a list of dictionaries.
import pandas as pd
df = pd.DataFrame(data)
print df
Its output is as follows −
a b c
0 1 2 NaN
1 5 10 20.0
Note − Observe, NaN (Not a Number) is appended in missing areas.
Example 2
The following example shows how to create a DataFrame by passing a list of dictionaries and
the row indices.
import pandas as pd
print df
Its output is as follows −
a b c
first 1 2 NaN
second 5 10 20.0
Example 3
The following example shows how to create a DataFrame with a list of dictionaries, row
indices, and column indices.
import pandas as pd
#With two column indices with one index with other name
print df1
print df2
Its output is as follows −
#df1 output
a b
first 1 2
second 5 10
#df2 output
a b1
first 1 NaN
second 5 NaN
Note − Observe, df2 DataFrame is created with a column index other than the dictionary key;
thus, appended the NaN’s in place. Whereas, df1 is created with column indices same as
dictionary keys, so NaN’s appended.
import pandas as pd
df = pd.DataFrame(d)
print df
Its output is as follows −
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
Note − Observe, for the series one, there is no label ‘d’ passed, but in the result, for the d label,
NaN is appended with NaN.
Column Selection
We will understand this by selecting a column from the DataFrame.
Example
import pandas as pd
df = pd.DataFrame(d)
print df ['one']
Its output is as follows −
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64
Column Addition
We will understand this by adding a new column to an existing data frame.
Example
import pandas as pd
df = pd.DataFrame(d)
# Adding a new column to an existing DataFrame object with column label by passing new
series
df['three']=pd.Series([10,20,30],index=['a','b','c'])
print df
df['four']=df['one']+df['three']
print df
Its output is as follows −
Example
import pandas as pd
df = pd.DataFrame(d)
print df
del df['one']
print df
df.pop('two')
print df
Its output is as follows −
Our dataframe is:
one three two
a 1.0 10.0 1
b 2.0 20.0 2
c 3.0 30.0 3
d NaN NaN 4
Selection by Label
Rows can be selected by passing row label to a loc function.
import pandas as pd
df = pd.DataFrame(d)
print df.loc['b']
Its output is as follows −
one 2.0
two 2.0
Name: b, dtype: float64
The result is a series with labels as column names of the DataFrame. And, the Name of the
series is the label with which it is retrieved.
import pandas as pd
df = pd.DataFrame(d)
print df.iloc[2]
Its output is as follows −
one 3.0
two 3.0
Name: c, dtype: float64
Slice Rows
Multiple rows can be selected using ‘ : ’ operator.
import pandas as pd
df = pd.DataFrame(d)
print df[2:4]
Its output is as follows −
one two
c 3.0 3
d NaN 4
Addition of Rows
Add new rows to a DataFrame using the append function. This function will append the rows at
the end.
import pandas as pd
df = df.append(df2)
print df
Its output is as follows −
a b
0 1 2
1 3 4
0 5 6
1 7 8
Deletion of Rows
Use index label to delete or drop rows from a DataFrame. If label is duplicated, then multiple
rows will be dropped.
If you observe, in the above example, the labels are duplicate. Let us drop a label and will see
how many rows will get dropped.
import pandas as pd
df = df.drop(0)
print df
Its output is as follows −
ab
134
178
In the above example, two rows were dropped because those two contain the same label 0.
Pandas. A panel is a 3D container of data. The term Panel data is derived from econometrics
and is partially responsible for the name pandas − pan(el)-da(ta)-s.
The names for the 3 axes are intended to give some semantic meaning to describing operations
involving panel data. They are −
pandas.Panel()
A Panel can be created using the following constructor −
Paramete Description
r
data Data takes various forms like ndarray, series, map, lists, dict, constants and
also another DataFrame
items axis=0
major_axis axis=1
minor_axis axis=2
Create Panel
A Panel can be created using multiple ways like −
From ndarrays
From dict of DataFrames
From 3D ndarray
import pandas as pd
import numpy as np
data = np.random.rand(2,4,5)
p = pd.Panel(data)
print p
Its output is as follows −
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)
Items axis: 0 to 1
Major_axis axis: 0 to 3
Minor_axis axis: 0 to 4
Note − Observe the dimensions of the empty panel and the above panel, all the objects are
different.
From dict of DataFrame Objects
import pandas as pd
import numpy as np
p = pd.Panel(data)
print p
Its output is as follows −
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)
Items axis: 0 to 1
Major_axis axis: 0 to 3
Minor_axis axis: 0 to 4
import pandas as pd
p = pd.Panel()
print p
Its output is as follows −
<class 'pandas.core.panel.Panel'>
Dimensions: 0 (items) x 0 (major_axis) x 0 (minor_axis)
Items axis: None
Major_axis axis: None
Minor_axis axis: None
import pandas as pd
import numpy as np
p = pd.Panel(data)
print p['Item1']
Its output is as follows −
0 1 2
0 0.488224 -0.128637 0.930817
1 0.417497 0.896681 0.576657
2 -2.775266 0.571668 0.290082
3 -0.400538 -0.144234 1.110535
We have two items, and we retrieved item1. The result is a DataFrame with 4 rows and 3
columns, which are the Major_axis and Minor_axis dimensions.
Using major_axis
Data can be accessed using the method panel.major_axis(index).
import pandas as pd
import numpy as np
p = pd.Panel(data)
print p.major_xs(1)
Its output is as follows −
Item1 Item2
0 0.417497 0.748412
1 0.896681 -0.557322
2 0.576657 NaN
Using minor_axis
Data can be accessed using the method panel.minor_axis(index).
import pandas as pd
import numpy as np
p = pd.Panel(data)
print p.minor_xs(1)
Its output is as follows −
Item1 Item2
0 -0.128637 -1.047032
1 0.896681 -0.557322
2 0.571668 0.431953
3 -0.144234 1.302466
Note − Observe the changes in the dimensions.
**************************************************************************
3. Data Input
This chapter shows how to read data into Python. Thus it forms the link between the chapter on
Python, and the first chapter on statistical data analysis. It may be surprising, but reading data
into the system in the correct format and checking for erroneous or missing entries is often one
of the most time consuming parts of the data analysis.
Data input can be complicated by a number of problems, like different separators between data
entries (such as spaces and/or tabulators), or empty lines at the end of the file. In addition, data
may have been saved in different formats, such as MS Excel, Matlab, HDF5 (which also
includes the Matlab-format), or in databases. Understandably, we cannot cover all possible input
options. But I will try to give an overview of where and how to start with data input.
When the data are available as ASCII-files, you should always start out with a visual inspection
of the data! In particular, you should check • Do the data have a header and/or a footer?
• Are there empty lines at the end of the file?
• Are there white-spaces before the first number, or at the end of each line? (The latter is a lot
harder to see.)
• Are the data separated by tabulators, and/or by spaces? (Tip: you should use a text-editor which
can visualize tabs, spaces, and end-of-line (EOL) characters.)
In Python, I strongly suggest that you start out reading in and inspecting your data in the Jupyter
QtConsole or in an Jupyter Notebook. It allows you to move around much more easily, try things
out, and quickly get feedback on how successful your commands have been. When you have
your command syntax worked out, you can obtain the command history with %history, copy it
into your favorite IDE, and turn it into a program.
While the a numpy command np.loadtxt allows to read in simply formatted text data, most of the
time I go straight to pandas, as it provides significantly more powerful tools for data-entry. A
typical workflow can contain the following steps:
After “In [7]” I often have to adjust the options of pd.read_csv, to read in all the data correctly.
Make sure you check that the number of column headers is equal to the number of columns that
you expect. It can happen that everything gets read in—but into one large single column!
Simple Text-Files
For example, a file data.txt with the following content
1, 1.3, 0.6
2, 2.1, 0.7
3, 4.8, 0.8
4, 3.3, 0.9
The pandas routine has the advantage that the first column is recognized as
integer, whereas the second and third columns are float.
The following commands show equivalent class and function approaches to read a single sheet:
*************************************************************************
4.1 Datatypes
The choice of appropriate statistical procedure depends on the data type. Data can be categorical
or numerical. If the variables are numerical, we are led to a certain statistical strategy. In
contrast, if the variables represent qualitative categorizations, then we follow a different path.
In addition, we distinguish between univariate, bivariate, and multivariate data. Univariate data
are data of only one variable, e.g., the size of a person. Bivariate data have two parameters, for
example, the x=y position in a plane, or the income as a function of age. Multivariate data have
three or more variables, e.g., the position of a particle in space, etc.
4.1.1 Categorical
a) Boolean
Boolean data are data which can only have two possible values.
For example,
• female/male
• smoker/nonsmoker
• True/False
b) Nominal
Many classifications require more than two categories. Such data are called nominal data. An
example is married/single/divorced.
c) Ordinal
In contrast to nominal data, ordinal data are ordered and have a logical sequence, e.g., very
few/few/some/many/very many.
4.1.2 Numerical
a) Numerical Continuous
Whenever possible, it is best to record the data in their original continuous format, and only with
a meaningful number of decimal places. For example, it does not make sense to record the body
size with more than 1mm accuracy, as there are larger changes in body height between the size in
the morning and the size in the evening, due to compression of the intervertebral disks.
b) Numerical Discrete
Some numerical data can only take on integer values. These data are called numerical discrete.
For example Number of children: 0 1 2 3 4 5 ….