04 Introduction To Python-1
04 Introduction To Python-1
1. Settings of Python
1.1 Installation of Python
Windows
To get started on Windows, download the Anaconda installer. Starting the Python IDE:
Spyder after installation.
You may also open the Command Prompt application (also known as cmd.exe), right-click
the Start menu and select Command Prompt. Try starting the Python interpreter by typing
python. You should see a message that matches the version of Anaconda you installed:
1|Page
UECM 1534 Programming Techniques for Data Processing Jan 18/19
C:\Users\wesm>python
Python 3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul 5 2016, 11:41:13)
[MSC v.1900 64 bit (AMD64)] on win32
>>>
To exit the shell, press Ctrl-D (on Linux or macOS), Ctrl-Z (on Windows), or type
the command exit() and press Enter.
To verify everything is working, try launching IPython in the system shell (open the Terminal
application to get a command prompt):
$ ipython
To exit the shell, press Ctrl-D or type exit() and press Enter.
results = []
for line in file_handle:
# keep the empty lines for now
# if len(line) == 0:
# continue
Comments can also occur after a line of executed code. While some programmers prefer
comments to be placed in the line preceding a particular line of code, this can be useful at
times:
You may import or load in the pandas library to your console using the following code
2|Page
UECM 1534 Programming Techniques for Data Processing Jan 18/19
Where pd is the abbreviated name of pandas that make coding short and simple. You can
choose other short name like pds as well. It is optional and depends on user preference.
Normally, it is import as pd. Thus, whenever you see pd. in code, it’s referring to pandas.
You may also find it easier to import Series and DataFrame into the local namespace
since they are so frequently used:
2.1 Series
A Series is a one-dimensional array-like object containing a sequence of values (of
similar types to Numpy types) and an associated array of data labels, called its index. The
simplest Series is formed from only an array of data:
In [4]: obj
Out[4]:
0 4
1 7
2 -5
3 3
dtype: int64
The output of Series shows the index on the left and the values on the right. Since we
did not specify an index for the data, a default one consisting of the integers 0 through N
- 1 (where N is the length of the data) is created. You can get the array representation and
index object of the Series via its values and index attributes (using period (.)).
In [5]: obj.values
Out[5]: array([ 4, 7, -5, 3])
3|Page
UECM 1534 Programming Techniques for Data Processing Jan 18/19
Often it will be desirable to create a Series with an index identifying each data point with
a label:
In [8]: obj2
Out[8]:
d 4
b 7
a -5
c 3
dtype: int64
In [9]: obj2.index
Out[9]: Index(['d', 'b', 'a', 'c'], dtype='object')
You can use labels in the index when selecting single values or a set of values from the
Series object using square parentheses ([ ]):
In [10]: obj2['a']
Out[10]: -5
In [11]: obj2['d'] = 6
There are others operation like filtering, scalar multiplication and so on as shown in the
following:
In [14]: obj2 * 2
Out[14]:
d 12
b 14
a -10
c 6
dtype: int64
4|Page
UECM 1534 Programming Techniques for Data Processing Jan 18/19
You can use an “in” operator to see if a particular element is inside the Series.
Should you have data contained in a Python dict, you can create a Series from it by
passing the dict:
In [20]: obj3
Out[20]:
Ohio 35000
Oregon 16000
Texas 71000
Utah 5000
dtype: int64
When you are only passing a dict, the index in the resulting Series will have the dict’s
keys in sorted order. You can override this by passing the dict keys in the order you want
them to appear in the resulting Series:
In [23]: obj4
Out[23]:
California NaN
Ohio 35000.0
Oregon 16000.0
Texas 71000.0
dtype: float64
5|Page
UECM 1534 Programming Techniques for Data Processing Jan 18/19
Here, three values found in sdata were placed in the appropriate locations, but since no
value for 'California' was found, it appears as NaN (not a number), which is considered in
pandas to mark missing or NA values. Since 'Utah' was not included in states, it is excluded
from the resulting object.
The terms “missing” or “NA” can be used interchangeably to refer to missing data. The
isnull and notnull functions in pandas should be used to detect missing data:
In [24]: pd.isnull(obj4)
Out[24]:
California True
Ohio False
Oregon False
Texas False
dtype: bool
In [25]: pd.notnull(obj4)
Out[25]:
California False
Ohio True
Oregon True
Texas True
dtype: bool
In [26]: obj4.isnull()
Out[26]:
California True
Ohio False
Oregon False
Texas False
dtype: bool
2.2 DataFrame
A DataFrame represents a rectangular table of data and contains an ordered collection of
columns, each of which can be a different value type (numeric, string, boolean, etc.). The
DataFrame has both a row and column index; it can be thought of as a dict of Series all
sharing the same index. Under the hood, the data is stored as one or more two-
dimensional blocks rather than a list, dict, or some other collection of one-dimensional
arrays.
There are many ways to construct a DataFrame, though one of the most common is
from a dict of equal-length lists or NumPy arrays:
6|Page
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [27]: data = {'state': ['Ohio', ' Ohio ', ' Ohio ', 'Nevada',
....:'Nevada', ' Nevada '], 'year': [2000, 2001, 2002, 2001,
....:2002, 2003], 'pop':[ 1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
In [28]: frame = pd.DataFrame(data)
The resulting DataFrame will have its index assigned automatically as with Series, and
the columns are placed in sorted order:
In [29]: frame
Out[29]:
pop state year
0 1.5 Ohio 2000
1 1.7 Ohio 2001
2 3.6 Ohio 2002
3 2.4 Nevada 2001
4 2.9 Nevada 2002
5 3.2 Nevada 2003
For large DataFrames, the .head() method selects only the first five rows:
In [30]: frame.head()
Out[30]:
pop state year
0 1.5 Ohio 2000
1 1.7 Ohio 2001
2 3.6 Ohio 2002
3 2.4 Nevada 2001
4 2.9 Nevada 2002
If you specify a sequence of columns, the DataFrame’s columns will be arranged in that
order:
If you pass a column that isn’t contained in the dict, it will appear with missing values
in the result:
In [32]: frame2 = pd.DataFrame(data, columns=['year', 'state',
....: 'pop', 'debt'], index=['one', 'two', 'three', 'four',
....: 'five', 'six'])
7|Page
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [33]: frame2
Out[33]:
year state pop debt
one 2000 Ohio 1.5 NaN
two 2001 Ohio 1.7 NaN
three 2002 Ohio 3.6 NaN
four 2001 Nevada 2.4 NaN
five 2002 Nevada 2.9 NaN
six 2003 Nevada 3.2 NaN
In [34]: frame2.columns
Out[34]: Index(['year', 'state', 'pop', 'debt'], dtype='object')
In [35]: frame2['state']
Out[35]:
one Ohio
two Ohio
three Ohio
four Nevada
five Nevada
six Nevada
Name: state, dtype: object
In [36]: frame2.year
Out[36]:
one 2000
two 2001
three 2002
four 2001
five 2002
six 2003
Name: year, dtype: int64
Attribute-like access (e.g., frame2.year) and tab completion of column names in IPython
is provided as a convenience. frame2[column] works for any column name.
Rows can also be retrieved by position or name with the special loc attribute:
In [37]: frame2.loc['three']
Out[37]:
year 2002
state Ohio
pop 3.6
debt NaN
Name: three, dtype: object
8|Page
UECM 1534 Programming Techniques for Data Processing Jan 18/19
Columns can be modified by assignment. For example, the empty 'debt' column could be
assigned a scalar value or an array of values:
In [39]: frame2
Out[39]:
year state pop debt
one 2000 Ohio 1.5 16.5
two 2001 Ohio 1.7 16.5
three 2002 Ohio 3.6 16.5
four 2001 Nevada 2.4 16.5
five 2002 Nevada 2.9 16.5
six 2003 Nevada 3.2 16.5
When you are assigning lists or arrays to a column, the value’s length must match the
length of the DataFrame.
In [41]: frame2
Out[41]:
year state pop debt
one 2000 Ohio 1.5 0.0
two 2001 Ohio 1.7 1.0
three 2002 Ohio 3.6 2.0
four 2001 Nevada 2.4 3.0
five 2002 Nevada 2.9 4.0
six 2003 Nevada 3.2 5.0
Assigning a column that doesn’t exist will create a new column. The del keyword will
delete columns as with a dict. As an example of del, a new column of boolean values where
the state column equals 'Ohio' is added:
In [43]: frame2
Out[43]:
year state pop debt eastern
one 2000 Ohio 1.5 NaN True
two 2001 Ohio 1.7 -1.2 True
three 2002 Ohio 3.6 NaN True
four 2001 Nevada 2.4 -1.5 False
five 2002 Nevada 2.9 -1.7 False
six 2003 Nevada 3.2 NaN False
New columns CANNOT be created with the frame2.eastern syntax. The del method can
then be used to remove this column:
9|Page
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [45]: frame2.columns
Out[45]: Index(['year', 'state', 'pop', 'debt'], dtype='object')
3. Essential Functionality
This section will walk you through the fundamental mechanics of interacting with the data
contained in a Series or DataFrame.
3.1 Reindexing
An important method on pandas objects is reindex, which means to create a new object
with the data conformed to a new index. Consider an example:
In [47]: obj
Out[47]:
d 4.5
b 7.2
a -5.3
c 3.6
dtype: float64
Calling .reindex() on this Series rearranges the data according to the new index,
introducing missing values if any index values were not already present:
In [49]: obj2
Out[49]:
a -5.3
b 7.2
c 3.6
d 4.5
e NaN
dtype: float64
With DataFrame, reindex can alter either the (row) index, columns, or both. When passed
only a sequence, it reindexes the rows in the result:
10 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [51]: frame
Out[51]:
Ohio Texas California
a 0 1 2
c 3 4 5
d 6 7 8
In [53]: frame2
Out[53]:
Ohio Texas California
a 0.0 1.0 2.0
b NaN NaN NaN
c 3.0 4.0 5.0
d 6.0 7.0 8.0
In [55]: frame.reindex(columns=states)
Out[55]:
Texas Utah California
a 1 NaN 2
c 4 NaN 5
d 7 NaN 8
In [57]: obj
Out[57]:
a 0.0
b 1.0
c 2.0
d 3.0
e 4.0
dtype: float64
11 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [59]: new_obj
Out[59]:
a 0.0
b 1.0
d 3.0
e 4.0
dtype: float64
With DataFrame, index values can be deleted from either axis. To illustrate this, we
first create an example DataFrame:
In [62]: data
Out[62]:
one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
Calling drop with a sequence of labels will drop values from the row labels (axis 0):
You can drop values from the columns by passing axis=1 or axis='columns':
12 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [67]: obj
Out[67]:
a 0.0
b 1.0
c 2.0
d 3.0
dtype: float64
In [68]: obj['b']
Out[68]: 1.0
In [69]: obj[1]
Out[69]: 1.0
In [70]: obj[2:4]
Out[70]:
c 2.0
d 3.0
dtype: float64
13 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
Slicing with labels behaves differently than normal Python slicing in that the endpoint is
inclusive:
In [74]: obj['b':'c']
Out[74]:
b 1.0
c 2.0
dtype: float64
Setting using these methods modifies the corresponding section of the Series:
In [75]: obj['b':'c'] = 5
In [76]: obj
Out[76]:
a 0.0
b 5.0
c 5.0
d 3.0
dtype: float64
Indexing into a DataFrame is for retrieving one or more columns either with a single
value or sequence:
In [78]: data
Out[78]:
one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
In [79]: data['two']
Out[79]:
Ohio 1
Colorado 5
Utah 9
New York 13
Name: two, dtype: int64
14 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
Indexing like this has a few special cases. First, slicing or selecting data with a boolean
array:
In [81]: data[:2]
Out[81]:
one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7
Another use case is in indexing with a boolean DataFrame, such as one produced by a
scalar comparison:
In [85]: data
Out[85]:
one two three four
Ohio 0 0 0 0
Colorado 0 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
15 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
As a preliminary example, let’s select a single row and multiple columns by label:
We’ll then perform some similar selections with integers using .iloc:
In [88]: data.iloc[2]
Out[88]:
one 8
two 9
three 10
four 11
Name: Utah, dtype: int64
Both indexing functions work with slices in addition to single labels or lists of labels:
16 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
So there are many ways to select and rearrange the data contained in a pandas object. For
DataFrame, Table below provides a short summary of many of them.
In [93]: frame
Out[93]:
b d e
Utah -0.204708 0.478943 -0.519439
Ohio -0.555730 1.965781 1.393406
Texas 0.092908 0.281746 0.769023
Oregon 1.246435 1.007189 -1.296221
In [94]: np.abs(frame)
Out[94]:
b d e
Utah 0.204708 0.478943 0.519439
Ohio 0.555730 1.965781 1.393406
Texas 0.092908 0.281746 0.769023
Oregon 1.246435 1.007189 1.296221
17 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [96]: frame.apply(f)
Out[96]:
b 1.802165
d 1.684034
e 2.689627
dtype: float64In
Here the function f, which computes the difference between the maximum and minimum
of a Series, is invoked once on each column in frame. The result is a Series having the
columns of frame as its index.
If you pass axis='columns'to apply.apply, the function will be invoked once per row
instead:
The function passed to apply need not return a scalar value; it can also return a Series
with multiple values:
In [99]: frame.apply(f)
Out[99]:
b d e
min -0.555730 0.281746 -1.296221
max 1.246435 1.965781 1.393406
Element-wise Python functions can be used, too. Suppose you wanted to compute a
formatted string from each floating-point value in frame. You can do this with .applymap:
18 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [101]: frame.applymap(format)
Out[101]:
b d e
Utah -0.20 0.48 -0.52
Ohio -0.56 1.97 1.39
Texas 0.09 0.28 0.77
Oregon 1.25 1.01 -1.30
The reason for the name .applymap is that Series has a .map method for applying an
element-wise function:
In [102]: frame['e'].map(format)
Out[102]:
Utah -0.52
Ohio 1.39
Texas 0.77
Oregon -1.30
Name: e, dtype: object
In [104]: obj.sort_index()
Out[104]:
a 1
b 2
c 3
d 0
dtype: int64
19 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [107]: frame.sort_index(axis=1)
Out[107]:
a b c d
three 1 2 3 0
one 5 6 7 4
The data is sorted in ascending order by default, but can be sorted in descending order,
too:
In [110]: obj.sort_values()
Out[110]:
2 -3
3 2
0 4
1 7
dtype: int64
Any missing values are sorted to the end of the Series by default:
In [112]: obj.sort_values()
Out[112]:
4 -3.0
5 2.0
0 4.0
2 7.0
1 NaN
3 NaN
dtype: float64
When sorting a DataFrame, you can use the data in one or more columns as the sort
keys. To do so, pass one or more column names to the by option of .sort_values:
20 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [114]: frame
Out[114]:
a b
0 0 4
1 1 7
2 0 -3
3 1 2
In [115]: frame.sort_values(by='b')
Out[115]:
a b
2 0 -3
3 1 2
0 0 4
1 1 7
Ranking assigns ranks from one through the number of valid data points in an array. The
rank methods for Series and DataFrame are the place to look; by default rank breaks
ties by assigning each group the mean rank:
In [118]: obj.rank()
Out[118]:
0 6.5
1 1.0
2 6.5
3 4.5
4 3.0
5 2.0
6 4.5
dtype: float64
Ranks can also be assigned according to the order in which they’re observed in thedata:
21 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [119]: obj.rank(method='first')
Out[119]:
0 6.0
1 1.0
2 7.0
3 4.0
4 3.0
5 2.0
6 5.0
dtype: float64
Here, instead of using the average rank 6.5 for the entries 0 and 2, they instead have been
set to 6 and 7 because label 0 precedes label 2 in the data.
In [122]: frame
Out[122]:
a b c
0 0 4.3 -2.0
1 1 7.0 5.0
2 0 -3.0 8.0
3 1 2.0 -2.5
In [123]: frame.rank(axis='columns')
Out[123]:
a b c
0 2.0 3.0 1.0
1 1.0 3.0 2.0
2 2.0 1.0 3.0
3 2.0 3.0 1.0
22 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [125]: df
Out[125]:
one two
a 1.40 NaN
b 7.10 -4.5
c NaN NaN
d 0.75 -1.3
In [126]: df.sum()
Out[126]:
one 9.25
two -5.80
dtype: float64
In [127]: df.sum(axis='columns')
Out[127]:
a 1.40
b 2.60
c NaN
d -0.55
dtype: float64
NA values are excluded unless the entire slice (row or column in this case) is NA. This can be
disabled with the skipna option:
23 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
Some methods, like .idxmin and .idxmax, return indirect statistics like the index value
where the minimum or maximum values are attained:
In [129]: df.idxmax()
Out[129]:
one b
two d
dtype: object
In [130]: df.cumsum()
Out[130]:
one two
a 1.40 NaN
b 8.50 -4.5
c NaN NaN
d 9.25 -5.8
In [131]: df.describe()
Out[131]:
one two
count 3.000000 2.000000
mean 3.083333 -2.900000
std 3.493685 2.262742
min 0.750000 -4.500000
25% 1.075000 -3.700000
50% 1.400000 -2.900000
75% 4.250000 -2.100000
max 7.100000 -1.300000
24 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [133]: obj.describe()
Out[133]:
count 16
unique 3
top a
freq 8
dtype: object
See table below for a full list of summary statistics and related methods.
25 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
pandas-datareader is useful in reading financial data from yahoo. To read particular stock
data, you can use the following code.
It’s possible that Yahoo! Finance no longer exists since Yahoo! was acquired by Verizon in
2017. Refer to the pandas-datareader documentation online for the latest functionality or
visit the link for fixing the problem https://www.youtube.com/watch?v=eSpH6fPd5Yw
Now, let’s compute percent changes of the prices, using method .pct_change:
In [139]: returns.tail()
Out[139]:
AAPL GOOG IBM MSFT
Date
2016-10-17 -0.000680 0.001837 0.002072 -0.003483
2016-10-18 -0.000681 0.019616 -0.026168 0.007690
2016-10-19 -0.002979 0.007846 0.003583 -0.002255
2016-10-20 -0.000512 -0.005652 0.001719 -0.004867
2016-10-21 -0.003930 0.003011 -0.012474 0.042096
The .corr method of Series computes the correlation of the overlapping, non-NA,
aligned-by-index values in two Series. Relatedly, .cov computes the covariance:
In [140]: returns['MSFT'].corr(returns['IBM'])
Out[140]: 0.49976361144151144
In [141]: returns['MSFT'].cov(returns['IBM'])
Out[141]: 8.8706554797035462e-05
DataFrame’s .corr and .cov methods, on the other hand, return a full correlation or
covariance matrix as a DataFrame, respectively:
26 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [142]: returns.corr()
Out[142]:
AAPL GOOG IBM MSFT
AAPL 1.000000 0.407919 0.386817 0.389695
GOOG 0.407919 1.000000 0.405099 0.465919
IBM 0.386817 0.405099 1.000000 0.499764
MSFT 0.389695 0.465919 0.499764 1.000000
In [143]: returns.cov()
Out[143]:
AAPL GOOG IBM MSFT
AAPL 0.000277 0.000107 0.000078 0.000095
GOOG 0.000107 0.000251 0.000078 0.000108
IBM 0.000078 0.000078 0.000146 0.000089
MSFT 0.000095 0.000108 0.000089 0.000215
Using DataFrame’s .corwith method, you can compute pairwise correlations between a
DataFrame’s columns or rows with another Series or DataFrame. Passing a DataFrame
computes the correlations of matching column names. Lets correlate the price and volume
of each stock:
In [144]: returns.corrwith(volume)
Out[144]:
AAPL -0.075565
GOOG -0.007067
IBM -0.204849
MSFT -0.092950
dtype: float64
In [145]: price.plot()
Out[145]:
27 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In [146]: obj = pd.Series(['c', 'a', 'd', 'a', 'a', 'b', 'b', 'c',
.....: 'c'])
The first function is .unique, which gives you an array of the unique values in a Series:
In [148]: uniques
Out[148]: array(['c', 'a', 'd', 'b'], dtype=object)
In [149]: obj.value_counts()
Out[149]:
c 3
a 3
b 2
d 1
dtype: int64
The .Index.get_indexer method, which gives you an index array from an array of
possibly non-distinct values into another array of distinct values:
In [153]: pd.Index(unique_vals).get_indexer(to_match)
Out[153]: array([0, 2, 1, 1, 0, 2])
28 | P a g e
UECM 1534 Programming Techniques for Data Processing Jan 18/19
In some cases, you may want to compute a histogram on multiple related columns in a
DataFrame. Here’s an example:
In [155]: data
Out[155]:
Qu1 Qu2 Qu3
0 1 2 1
1 3 3 5
2 4 1 2
3 3 2 4
4 4 3 4
In [157]: result
Out[157]:
Qu1 Qu2 Qu3
1 1.0 1.0 1.0
2 0.0 2.0 1.0
3 2.0 2.0 0.0
4 2.0 0.0 2.0
5 0.0 0.0 1.0
Here, the row labels in the result are the distinct values occurring in all of the columns.
The values are the respective counts of these values in each column.
29 | P a g e