Python Tutorial 7: Libraries

This is the highlight of learning Python. We have went through the use of numerous in-built functions and also user defined functions. You would have realised that these types of function represents the two extremes in the use of technology: Either as an end users who utilise what other people have coded for you, or code the logic of a program from scratch. One is convenient but inflexible, the other offers ultimate flexibility but you probably have to go through a 4 year degree code up a complex algorithm.

Programmers over the years also realised this issue as well, when they are spending too much time coding things from ground up. Instead of reinventing the wheel, they embraced the open-source and decided to pool together their code that they have optimised for their line of work.

Since these libaries are free for all to use (without copyright issues as they are merely logical steps complied for easy use), this is where non computing students should learn to code and program our own custom solutions for our work.

In Business Analytics Consulting Team, we pride ourselves in knowing enough to apply many machine learning algorithmn and data analytics tools and explain the signficance of these applications to actual business operations, be it marketing, finance, adminstration or HR. We are less concerned about how it is done, but why should it be done.

Installation of packages and importing of modules

There are external Python libaries you can download and install on your local Python installation. There are also internal Python libaries that does not require download but is not usually active unless you call for it.

Internal Libraries

For example, you might always use excel files to analyse CSV files. However, given the massive influx of data in the real business world, a business analyst might be faced with a dataset of more than 1,048,576 rows (Excel’s row limit). Even if your dataset might not even reach 1 million rows, a couple hundred thousand rows of data can already paralyse your computer if you try to manipulate it in excel.

Hence we will cover an example of an internal libary that can used to read csv files in to Python – csv

In [3]:
import csv
#now you can take input from csv files!
myCSVfile=open("user.CSV", "r")
#open() function takes in the path/name of file, and what action you want to do to it, r stands for read
reader = csv.reader(myCSVfile)
for row in reader:
print(row)
['id', 'first_name', 'last_name', 'gender', 'age', 'email', 'ip_address', 'created_time']
['1', 'Lyda', 'Clues', 'Female', '32', 'lclues0@java.com', '7.56.94.77', '2017-10-06 20:45:51']
['2', 'Demetrius', 'Franks', 'Male', '45', 'dfranks1@livejournal.com', '219.3.208.171', '2017-11-09 18:43:57']
['3', 'Lebbie', 'Laurent', 'Female', '35', 'llaurent2@huffingtonpost.com', '196.15.212.128', '2017-12-04 19:59:39']
['4', 'Liana', 'Gawen', 'Female', '50', 'lgawen3@google.com', '58.216.62.115', '2018-01-19 02:52:57']
['5', 'Tymon', 'Huggon', 'Male', '29', 'thuggon4@goo.ne.jp', '127.251.184.252', '2018-02-27 04:35:50']
['6', 'Jessalin', 'Scini', 'Female', '32', 'jscini5@blogger.com', '80.220.20.106', '2017-08-28 14:03:43']
['7', 'Ronda', 'Dockrill', 'Female', '36', 'rdockrill6@webs.com', '241.56.47.132', '2017-06-10 06:11:08']
['8', 'Ashien', 'Glidder', 'Female', '', 'aglidder7@github.io', '149.60.3.61', '2017-06-23 12:54:50']
['9', 'Cecilla', 'Josefowicz', 'Female', '25', 'cjosefowicz8@ebay.com', '168.34.20.228', '2017-07-13 19:26:57']
['10', 'Brigg', 'Micklewright', 'Male', '', 'bmicklewright9@boston.com', '25.252.116.121', '2018-04-07 02:05:13']
['11', 'Avrit', 'Wrangle', 'Female', '33', 'awranglea@home.pl', '80.161.183.123', '2017-08-18 09:01:28']
['12', 'Isidora', 'Bofield', 'Female', '43', 'ibofieldb@freewebs.com', '214.37.163.204', '2017-09-25 18:54:48']
['13', 'Gabriel', 'Narramore', 'Female', '47', 'gnarramorec@ft.com', '69.134.203.85', '2017-12-25 19:53:35']
['14', 'Murray', 'Finney', 'Male', '43', 'mfinneyd@webeden.co.uk', '33.137.59.171', '2018-01-22 10:01:28']
['15', 'Diann', 'Pauletti', 'Female', '39', 'dpaulettie@gov.uk', '25.191.8.98', '2017-11-28 03:48:51']
['16', 'Papagena', 'Rooper', 'Female', '38', 'prooperf@forbes.com', '0.230.87.20', '2018-05-10 07:49:21']
['17', 'Tremaine', 'Lacoste', 'Male', '34', 'tlacosteg@reuters.com', '85.2.94.72', '2017-05-29 23:37:21']
['18', 'Lanita', 'Suddaby', 'Female', '43', 'lsuddabyh@imgur.com', '127.212.164.138', '2017-06-03 03:26:44']
['19', 'Jeralee', 'Eslemont', 'Female', '41', 'jeslemonti@yahoo.co.jp', '110.91.134.83', '2018-04-23 22:19:14']
['20', 'Rabi', 'Gander', 'Male', '47', 'rganderj@comcast.net', '206.195.153.8', '2017-09-06 16:09:36']
['21', 'Ragnar', 'Betser', 'Male', '32', 'rbetserk@themeforest.net', '39.133.247.86', '2018-03-14 12:08:18']
['22', 'Cecilio', 'Burke', 'Male', '50', 'cburkel@ox.ac.uk', '51.228.253.81', '2017-08-02 14:50:49']
['23', 'Anselm', 'Candish', 'Male', '45', 'acandishm@google.pl', '119.114.228.222', '2017-12-16 02:30:55']
['24', 'Connor', 'Cominetti', 'Male', '40', 'ccominettin@privacy.gov.au', '94.54.250.106', '2017-05-22 01:07:00']
['25', 'Mario', 'Vannuchi', 'Male', '48', 'mvannuchio@symantec.com', '36.154.173.198', '2017-07-14 06:01:40']
['26', 'Viola', 'Dedon', 'Female', '', 'vdedonp@deviantart.com', '98.161.127.34', '2017-08-02 20:26:21']
['27', 'Augustin', 'Beszant', 'Male', '32', 'abeszantq@state.gov', '202.219.140.45', '2017-11-08 11:42:34']
['28', 'Gill', 'Porteous', 'Male', '', 'gporteousr@unblog.fr', '15.2.75.24', '2018-04-09 17:45:40']
.... and many more rows

External Libraries

WebScraping – Scrapy

Machine Learning Algorithms – sklearn

Data Visualisation – matplotlib

You can install them through cmd (windows) / terminal (MacOS) with the command pip install packagename.

For example: pip install pandas (https://stackoverflow.com/questions/42907331/how-to-install-pandas-from-pip-on-windows-cmd)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s