import csv
4 Lab: Libraries
Welcome to the third part of IM939 lab 1. Here we are going to look at libraries and how they extend Python’s core features.
4.1 Modules
A module is a collection of code someone else has written (objects, methods, etc.). A library is a group of modules. In data science work you will use libraries such as numpy
and scikit-learn
. As we mentioned, Anaconda has many of these libraries built in and we do not need to install them separately.
Sometimes there are multiple ways to do something. Different libraries may have modules which have similiar functionality but slight differences.
For example, lets go through a comma seperated value file to make some data accessible in Python.
4.1.1 CSV module
The import keyword loads in a module. Here it loads in the csv module from the inbuilt Python library (see the Python docs on the csv module).
Please put the Facebook.csv file in the same folder as this notebook for this bit.
The code for going through a csv file is a bit lengthy.
with open('data/Facebook.csv', mode = 'r', encoding = 'UTF-8') as csvfile: # open up our csv file
= csv.reader(csvfile) # create a reader object which looks at our csv file
reader for row in reader: # for each row the reader finds
print(row) # print out the current row
['\ufefftype', 'post_message', 'link', 'link_domain', 'post_published', 'likes_count_fb', 'comments_count_fb', 'reactions_count_fb', 'shares_count_fb', 'engagement_fb']
['status', 'Don t forget to post your photographs from heritage weekend of you having a good time on here please.', '', '', '2017-09-09T13:57:14+0000', '5', '1', '5', '0', '6']
['status', 'If you want to learn about Historic Coventry this Saturday we will be carrying out a guided tour around the Medieval Wall of Coventry. Booking details below', '', '', '2017-08-31T17:36:47+0000', '7', '12', '7', '0', '19']
['event', '3 more dates for September Tours of The Medieval City Wall Below. Saturday 16th 23 rd and 30th https://www.facebook.com/events/1935828209971181/?ti=icl', 'https://www.facebook.com/events/1935828209971181/', 'facebook.com', '2017-09-05T10:46:45+0000', '2', '0', '2', '0', '2']
['link', 'http://www.coventrytelegraph.net/whats-on/arts-culture-news/majestic-medieval-hall-coventrys-best-13448249', 'http://www.coventrytelegraph.net/whats-on/arts-culture-news/majestic-medieval-hall-coventrys-best-13448249', 'coventrytelegraph.net', '2017-08-14T08:56:04+0000', '9', '2', '10', '4', '16']
['status', 'Different angle of St Michaels', '', '', '2017-08-26T09:04:35+0000', '11', '0', '12', '0', '12']
['link', 'The Heritage Open Days are taking place from 7-10 September with venues opening their doors and giving access to areas not normally open to the public - and now is the time to book a tour. As in previous years there are some very interesting and unique tours taking place. Make sure you don’t miss out on these by checking booking details online and booking your place. Tours that need pre booking include: Medieval City Wall; BBC Coventry and Warwickshire Radio; Priory Undercroft and Visitor Centre; King Henry VIII School; Coventry Solihull Waste Disposal Ltd; and Coventry Central Library behind the scenes tour. A Heritage Open Days brochure will be available from Council buildings The Herbert Coventry Cathedral and libraries from Wednesday (23 August). For more details visit the website www.coventry.gov.uk/hod.', 'http://www.coventry.gov.uk/hod', 'coventry.gov.uk', '2017-08-21T10:26:52+0000', '10', '0', '10', '13', '23']
['status', 'Hi thanks to be able to share here & be part of tnis group ; Good to kbow iof yt area as going down soon with Natiomal Trust centre from Glasgow', '', '', '2017-08-17T08:36:07+0000', '1', '8', '1', '0', '9']
['link', 'http://www.coventrytelegraph.net/whats-on/whats-on-news/future-coventrys-albany-theatre-been-13477790', 'http://www.coventrytelegraph.net/whats-on/whats-on-news/future-coventrys-albany-theatre-been-13477790', 'coventrytelegraph.net', '2017-08-17T09:14:59+0000', '8', '1', '9', '1', '11']
['photo', 'https://www.facebook.com/peter.garbett.9/posts/1769150796433812 Sad we are loosing this museum visit it whilst you can', 'https://www.facebook.com/photo.php?fbid=1280170205424760&set=gm.1921178978100268&type=3', 'facebook.com', '2017-08-10T11:49:47+0000', '2', '1', '2', '0', '3']
['link', 'https://www.facebook.com/CoventryPVC/posts/859986554155927', 'http://www.crowdfunder.co.uk/coventry-priory', 'crowdfunder.co.uk', '2017-08-07T21:13:46+0000', '6', '1', '6', '0', '7']
['photo', 'Looking good in the sunshine.. https://www.facebook.com/visitcoventry.net/posts/501006916915750', 'https://www.facebook.com/visitcoventry.net/photos/a.373019843047792.1073741829.361929627490147/501006773582431/?type=3', 'facebook.com', '2017-08-06T21:25:32+0000', '8', '0', '8', '1', '9']
['status', 'Best unique Pubs Whats your favourite pubs and why? in Coventry and surrounding areas.', '', '', '2017-07-19T08:35:49+0000', '0', '7', '0', '0', '7']
['status', 'Thank you for letting me join your group', '', '', '2017-07-19T14:30:58+0000', '1', '1', '1', '0', '2']
['link', 'https://www.facebook.com/theprideofwillenhall/posts/1378187435629358', 'http://www.coventrytelegraph.net/whats-on/whats-on-news/worlds-longest-inflatable-obstacle-course-13346712', 'coventrytelegraph.net', '2017-07-19T08:23:55+0000', '0', '0', '0', '0', '0']
['link', 'https://www.facebook.com/peter.garbett.9/posts/1740635729285319 Coventry shortlisted!', 'https://coventry2021.co.uk/news/?is_mobile=1', 'coventry2021.co.uk', '2017-07-15T18:04:34+0000', '2', '0', '2', '0', '2']
['photo', 'Nice. https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/489764101373365/?type=3', 'https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/489764101373365/?type=3', 'facebook.com', '2017-07-13T22:06:20+0000', '3', '0', '3', '0', '3']
['photo', 'I ve always wanted to have a nose down this passageway.. https://www.facebook.com/visitcoventry.net/posts/487218148294627', 'https://www.facebook.com/visitcoventry.net/photos/a.373019843047792.1073741829.361929627490147/487215948294847/?type=3', 'facebook.com', '2017-07-09T12:21:01+0000', '11', '1', '11', '0', '12']
['link', 'http://www.coventrytelegraph.net/whats-on/music-nightlife-news/what-godiva-festival-weather-forecast-13275038', 'http://www.coventrytelegraph.net/whats-on/music-nightlife-news/what-godiva-festival-weather-forecast-13275038', 'coventrytelegraph.net', '2017-07-08T08:39:38+0000', '1', '0', '1', '0', '1']
['video', 'Looks like a great idea.', 'https://www.facebook.com/stmarysguildhall/videos/10156233759843475/', 'facebook.com', '2017-07-07T15:28:24+0000', '7', '0', '9', '2', '11']
['photo', 'Lovely pic.. https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/483673398649102/?type=3', 'https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/483673398649102/?type=3', 'facebook.com', '2017-07-02T08:41:08+0000', '15', '2', '16', '1', '19']
['status', 'What is your favourite restaurant or eatery in Coventry area?', '', '', '2017-06-29T08:55:29+0000', '3', '35', '3', '0', '38']
['photo', 'Interesting.. https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/481529385530170/?type=3', 'https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/481529385530170/?type=3', 'facebook.com', '2017-06-29T05:32:53+0000', '9', '0', '9', '0', '9']
['link', 'https://www.facebook.com/BerkswellWindmill/posts/1322847697832828', 'https://www.facebook.com/BerkswellWindmill/posts/1322847697832828', 'facebook.com', '2017-06-26T18:24:47+0000', '0', '0', '0', '0', '0']
['status', 'Just saw a post about people that made me think would almost be a great motto for our fabulous city. Let go of what s gone. Be grateful for (and save) what remains. Look forward to what is coming. ', '', '', '2017-06-25T17:17:12+0000', '4', '2', '5', '0', '7']
['event', 'Why not make a weekend of it? It s Jaguar Super Saturday! In aid of Air Ambulance details on joining below if you own a Jaguar! This year there will be a timeline of cars from oldest to newest many built at Browns Lane!', 'https://www.facebook.com/events/201361447045208/', 'facebook.com', '2017-06-22T19:27:54+0000', '2', '0', '2', '0', '2']
['event', ':) we have a wonderful collection of exciting Citroen motorcars at the Museum on Sunday if you own a Citroen you re welcome to come along with your car 10-4pm it s FREE and you may be given a leaflet about the car club! Any Citroen can come along old or new thank you :)', 'https://www.facebook.com/events/362084697478113/', 'facebook.com', '2017-06-22T19:23:49+0000', '1', '0', '1', '0', '1']
['event', 'City Wall Guided Costume Tour this Saturday meeting at 2pm at Priory Visitors Centre ( behind Nando s Trinity Street) with the Deep Fat Friar https://www.facebook.com/events/313502545772268/?ti=icl', 'https://www.facebook.com/events/313502545772268/', 'facebook.com', '2017-06-22T19:20:55+0000', '6', '0', '6', '1', '7']
['link', 'https://www.facebook.com/BerkswellWindmill/posts/1322847697832828', 'https://www.facebook.com/BerkswellWindmill/posts/1322847697832828', 'facebook.com', '2017-06-22T16:59:56+0000', '1', '0', '1', '0', '1']
Ok, each row looks like a list. But printing it out is pretty messy. We should dump this into a list of dictionaries (so we can pick out particular values).
= [] # create list to put our data into
csv_content
with open('data/Facebook.csv', mode = 'r', encoding = 'UTF-8') as csvfile: # open up csv file
= csv.DictReader(csvfile) # create a reader which will read in each row and turn it into a dictionary
reader for row in reader: # for each row
csv_content.append(row) print(row) # put the created dictionary into our list
{'\ufefftype': 'status', 'post_message': 'Don t forget to post your photographs from heritage weekend of you having a good time on here please.', 'link': '', 'link_domain': '', 'post_published': '2017-09-09T13:57:14+0000', 'likes_count_fb': '5', 'comments_count_fb': '1', 'reactions_count_fb': '5', 'shares_count_fb': '0', 'engagement_fb': '6'}
{'\ufefftype': 'status', 'post_message': 'If you want to learn about Historic Coventry this Saturday we will be carrying out a guided tour around the Medieval Wall of Coventry. Booking details below', 'link': '', 'link_domain': '', 'post_published': '2017-08-31T17:36:47+0000', 'likes_count_fb': '7', 'comments_count_fb': '12', 'reactions_count_fb': '7', 'shares_count_fb': '0', 'engagement_fb': '19'}
{'\ufefftype': 'event', 'post_message': '3 more dates for September Tours of The Medieval City Wall Below. Saturday 16th 23 rd and 30th https://www.facebook.com/events/1935828209971181/?ti=icl', 'link': 'https://www.facebook.com/events/1935828209971181/', 'link_domain': 'facebook.com', 'post_published': '2017-09-05T10:46:45+0000', 'likes_count_fb': '2', 'comments_count_fb': '0', 'reactions_count_fb': '2', 'shares_count_fb': '0', 'engagement_fb': '2'}
{'\ufefftype': 'link', 'post_message': 'http://www.coventrytelegraph.net/whats-on/arts-culture-news/majestic-medieval-hall-coventrys-best-13448249', 'link': 'http://www.coventrytelegraph.net/whats-on/arts-culture-news/majestic-medieval-hall-coventrys-best-13448249', 'link_domain': 'coventrytelegraph.net', 'post_published': '2017-08-14T08:56:04+0000', 'likes_count_fb': '9', 'comments_count_fb': '2', 'reactions_count_fb': '10', 'shares_count_fb': '4', 'engagement_fb': '16'}
{'\ufefftype': 'status', 'post_message': 'Different angle of St Michaels', 'link': '', 'link_domain': '', 'post_published': '2017-08-26T09:04:35+0000', 'likes_count_fb': '11', 'comments_count_fb': '0', 'reactions_count_fb': '12', 'shares_count_fb': '0', 'engagement_fb': '12'}
{'\ufefftype': 'link', 'post_message': 'The Heritage Open Days are taking place from 7-10 September with venues opening their doors and giving access to areas not normally open to the public - and now is the time to book a tour. As in previous years there are some very interesting and unique tours taking place. Make sure you don’t miss out on these by checking booking details online and booking your place. Tours that need pre booking include: Medieval City Wall; BBC Coventry and Warwickshire Radio; Priory Undercroft and Visitor Centre; King Henry VIII School; Coventry Solihull Waste Disposal Ltd; and Coventry Central Library behind the scenes tour. A Heritage Open Days brochure will be available from Council buildings The Herbert Coventry Cathedral and libraries from Wednesday (23 August). For more details visit the website www.coventry.gov.uk/hod.', 'link': 'http://www.coventry.gov.uk/hod', 'link_domain': 'coventry.gov.uk', 'post_published': '2017-08-21T10:26:52+0000', 'likes_count_fb': '10', 'comments_count_fb': '0', 'reactions_count_fb': '10', 'shares_count_fb': '13', 'engagement_fb': '23'}
{'\ufefftype': 'status', 'post_message': 'Hi thanks to be able to share here & be part of tnis group ; Good to kbow iof yt area as going down soon with Natiomal Trust centre from Glasgow', 'link': '', 'link_domain': '', 'post_published': '2017-08-17T08:36:07+0000', 'likes_count_fb': '1', 'comments_count_fb': '8', 'reactions_count_fb': '1', 'shares_count_fb': '0', 'engagement_fb': '9'}
{'\ufefftype': 'link', 'post_message': 'http://www.coventrytelegraph.net/whats-on/whats-on-news/future-coventrys-albany-theatre-been-13477790', 'link': 'http://www.coventrytelegraph.net/whats-on/whats-on-news/future-coventrys-albany-theatre-been-13477790', 'link_domain': 'coventrytelegraph.net', 'post_published': '2017-08-17T09:14:59+0000', 'likes_count_fb': '8', 'comments_count_fb': '1', 'reactions_count_fb': '9', 'shares_count_fb': '1', 'engagement_fb': '11'}
{'\ufefftype': 'photo', 'post_message': 'https://www.facebook.com/peter.garbett.9/posts/1769150796433812 Sad we are loosing this museum visit it whilst you can', 'link': 'https://www.facebook.com/photo.php?fbid=1280170205424760&set=gm.1921178978100268&type=3', 'link_domain': 'facebook.com', 'post_published': '2017-08-10T11:49:47+0000', 'likes_count_fb': '2', 'comments_count_fb': '1', 'reactions_count_fb': '2', 'shares_count_fb': '0', 'engagement_fb': '3'}
{'\ufefftype': 'link', 'post_message': 'https://www.facebook.com/CoventryPVC/posts/859986554155927', 'link': 'http://www.crowdfunder.co.uk/coventry-priory', 'link_domain': 'crowdfunder.co.uk', 'post_published': '2017-08-07T21:13:46+0000', 'likes_count_fb': '6', 'comments_count_fb': '1', 'reactions_count_fb': '6', 'shares_count_fb': '0', 'engagement_fb': '7'}
{'\ufefftype': 'photo', 'post_message': 'Looking good in the sunshine.. https://www.facebook.com/visitcoventry.net/posts/501006916915750', 'link': 'https://www.facebook.com/visitcoventry.net/photos/a.373019843047792.1073741829.361929627490147/501006773582431/?type=3', 'link_domain': 'facebook.com', 'post_published': '2017-08-06T21:25:32+0000', 'likes_count_fb': '8', 'comments_count_fb': '0', 'reactions_count_fb': '8', 'shares_count_fb': '1', 'engagement_fb': '9'}
{'\ufefftype': 'status', 'post_message': 'Best unique Pubs Whats your favourite pubs and why? in Coventry and surrounding areas.', 'link': '', 'link_domain': '', 'post_published': '2017-07-19T08:35:49+0000', 'likes_count_fb': '0', 'comments_count_fb': '7', 'reactions_count_fb': '0', 'shares_count_fb': '0', 'engagement_fb': '7'}
{'\ufefftype': 'status', 'post_message': 'Thank you for letting me join your group', 'link': '', 'link_domain': '', 'post_published': '2017-07-19T14:30:58+0000', 'likes_count_fb': '1', 'comments_count_fb': '1', 'reactions_count_fb': '1', 'shares_count_fb': '0', 'engagement_fb': '2'}
{'\ufefftype': 'link', 'post_message': 'https://www.facebook.com/theprideofwillenhall/posts/1378187435629358', 'link': 'http://www.coventrytelegraph.net/whats-on/whats-on-news/worlds-longest-inflatable-obstacle-course-13346712', 'link_domain': 'coventrytelegraph.net', 'post_published': '2017-07-19T08:23:55+0000', 'likes_count_fb': '0', 'comments_count_fb': '0', 'reactions_count_fb': '0', 'shares_count_fb': '0', 'engagement_fb': '0'}
{'\ufefftype': 'link', 'post_message': 'https://www.facebook.com/peter.garbett.9/posts/1740635729285319 Coventry shortlisted!', 'link': 'https://coventry2021.co.uk/news/?is_mobile=1', 'link_domain': 'coventry2021.co.uk', 'post_published': '2017-07-15T18:04:34+0000', 'likes_count_fb': '2', 'comments_count_fb': '0', 'reactions_count_fb': '2', 'shares_count_fb': '0', 'engagement_fb': '2'}
{'\ufefftype': 'photo', 'post_message': 'Nice. https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/489764101373365/?type=3', 'link': 'https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/489764101373365/?type=3', 'link_domain': 'facebook.com', 'post_published': '2017-07-13T22:06:20+0000', 'likes_count_fb': '3', 'comments_count_fb': '0', 'reactions_count_fb': '3', 'shares_count_fb': '0', 'engagement_fb': '3'}
{'\ufefftype': 'photo', 'post_message': 'I ve always wanted to have a nose down this passageway.. https://www.facebook.com/visitcoventry.net/posts/487218148294627', 'link': 'https://www.facebook.com/visitcoventry.net/photos/a.373019843047792.1073741829.361929627490147/487215948294847/?type=3', 'link_domain': 'facebook.com', 'post_published': '2017-07-09T12:21:01+0000', 'likes_count_fb': '11', 'comments_count_fb': '1', 'reactions_count_fb': '11', 'shares_count_fb': '0', 'engagement_fb': '12'}
{'\ufefftype': 'link', 'post_message': 'http://www.coventrytelegraph.net/whats-on/music-nightlife-news/what-godiva-festival-weather-forecast-13275038', 'link': 'http://www.coventrytelegraph.net/whats-on/music-nightlife-news/what-godiva-festival-weather-forecast-13275038', 'link_domain': 'coventrytelegraph.net', 'post_published': '2017-07-08T08:39:38+0000', 'likes_count_fb': '1', 'comments_count_fb': '0', 'reactions_count_fb': '1', 'shares_count_fb': '0', 'engagement_fb': '1'}
{'\ufefftype': 'video', 'post_message': 'Looks like a great idea.', 'link': 'https://www.facebook.com/stmarysguildhall/videos/10156233759843475/', 'link_domain': 'facebook.com', 'post_published': '2017-07-07T15:28:24+0000', 'likes_count_fb': '7', 'comments_count_fb': '0', 'reactions_count_fb': '9', 'shares_count_fb': '2', 'engagement_fb': '11'}
{'\ufefftype': 'photo', 'post_message': 'Lovely pic.. https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/483673398649102/?type=3', 'link': 'https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/483673398649102/?type=3', 'link_domain': 'facebook.com', 'post_published': '2017-07-02T08:41:08+0000', 'likes_count_fb': '15', 'comments_count_fb': '2', 'reactions_count_fb': '16', 'shares_count_fb': '1', 'engagement_fb': '19'}
{'\ufefftype': 'status', 'post_message': 'What is your favourite restaurant or eatery in Coventry area?', 'link': '', 'link_domain': '', 'post_published': '2017-06-29T08:55:29+0000', 'likes_count_fb': '3', 'comments_count_fb': '35', 'reactions_count_fb': '3', 'shares_count_fb': '0', 'engagement_fb': '38'}
{'\ufefftype': 'photo', 'post_message': 'Interesting.. https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/481529385530170/?type=3', 'link': 'https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/481529385530170/?type=3', 'link_domain': 'facebook.com', 'post_published': '2017-06-29T05:32:53+0000', 'likes_count_fb': '9', 'comments_count_fb': '0', 'reactions_count_fb': '9', 'shares_count_fb': '0', 'engagement_fb': '9'}
{'\ufefftype': 'link', 'post_message': 'https://www.facebook.com/BerkswellWindmill/posts/1322847697832828', 'link': 'https://www.facebook.com/BerkswellWindmill/posts/1322847697832828', 'link_domain': 'facebook.com', 'post_published': '2017-06-26T18:24:47+0000', 'likes_count_fb': '0', 'comments_count_fb': '0', 'reactions_count_fb': '0', 'shares_count_fb': '0', 'engagement_fb': '0'}
{'\ufefftype': 'status', 'post_message': 'Just saw a post about people that made me think would almost be a great motto for our fabulous city. Let go of what s gone. Be grateful for (and save) what remains. Look forward to what is coming. ', 'link': '', 'link_domain': '', 'post_published': '2017-06-25T17:17:12+0000', 'likes_count_fb': '4', 'comments_count_fb': '2', 'reactions_count_fb': '5', 'shares_count_fb': '0', 'engagement_fb': '7'}
{'\ufefftype': 'event', 'post_message': 'Why not make a weekend of it? It s Jaguar Super Saturday! In aid of Air Ambulance details on joining below if you own a Jaguar! This year there will be a timeline of cars from oldest to newest many built at Browns Lane!', 'link': 'https://www.facebook.com/events/201361447045208/', 'link_domain': 'facebook.com', 'post_published': '2017-06-22T19:27:54+0000', 'likes_count_fb': '2', 'comments_count_fb': '0', 'reactions_count_fb': '2', 'shares_count_fb': '0', 'engagement_fb': '2'}
{'\ufefftype': 'event', 'post_message': ':) we have a wonderful collection of exciting Citroen motorcars at the Museum on Sunday if you own a Citroen you re welcome to come along with your car 10-4pm it s FREE and you may be given a leaflet about the car club! Any Citroen can come along old or new thank you :)', 'link': 'https://www.facebook.com/events/362084697478113/', 'link_domain': 'facebook.com', 'post_published': '2017-06-22T19:23:49+0000', 'likes_count_fb': '1', 'comments_count_fb': '0', 'reactions_count_fb': '1', 'shares_count_fb': '0', 'engagement_fb': '1'}
{'\ufefftype': 'event', 'post_message': 'City Wall Guided Costume Tour this Saturday meeting at 2pm at Priory Visitors Centre ( behind Nando s Trinity Street) with the Deep Fat Friar https://www.facebook.com/events/313502545772268/?ti=icl', 'link': 'https://www.facebook.com/events/313502545772268/', 'link_domain': 'facebook.com', 'post_published': '2017-06-22T19:20:55+0000', 'likes_count_fb': '6', 'comments_count_fb': '0', 'reactions_count_fb': '6', 'shares_count_fb': '1', 'engagement_fb': '7'}
{'\ufefftype': 'link', 'post_message': 'https://www.facebook.com/BerkswellWindmill/posts/1322847697832828', 'link': 'https://www.facebook.com/BerkswellWindmill/posts/1322847697832828', 'link_domain': 'facebook.com', 'post_published': '2017-06-22T16:59:56+0000', 'likes_count_fb': '1', 'comments_count_fb': '0', 'reactions_count_fb': '1', 'shares_count_fb': '0', 'engagement_fb': '1'}
Look at the first entry in csv_content.
0] csv_content[
{'\ufefftype': 'status',
'post_message': 'Don t forget to post your photographs from heritage weekend of you having a good time on here please.',
'link': '',
'link_domain': '',
'post_published': '2017-09-09T13:57:14+0000',
'likes_count_fb': '5',
'comments_count_fb': '1',
'reactions_count_fb': '5',
'shares_count_fb': '0',
'engagement_fb': '6'}
Dictionaries have keys. The keys method of the dictionary object will show them to us (though they are obvious from printing the first line aboe).
0].keys() csv_content[
dict_keys(['\ufefftype', 'post_message', 'link', 'link_domain', 'post_published', 'likes_count_fb', 'comments_count_fb', 'reactions_count_fb', 'shares_count_fb', 'engagement_fb'])
0]['post_message'] csv_content[
'Don t forget to post your photographs from heritage weekend of you having a good time on here please.'
We have a list where each list element is a dictionary. So we need to index our list each time, hence the csv_content[0].
To go through our list we need to use a for loop. So, in order to get each post message.
for post in csv_content:
print(post['post_message'])
Don t forget to post your photographs from heritage weekend of you having a good time on here please.
If you want to learn about Historic Coventry this Saturday we will be carrying out a guided tour around the Medieval Wall of Coventry. Booking details below
3 more dates for September Tours of The Medieval City Wall Below. Saturday 16th 23 rd and 30th https://www.facebook.com/events/1935828209971181/?ti=icl
http://www.coventrytelegraph.net/whats-on/arts-culture-news/majestic-medieval-hall-coventrys-best-13448249
Different angle of St Michaels
The Heritage Open Days are taking place from 7-10 September with venues opening their doors and giving access to areas not normally open to the public - and now is the time to book a tour. As in previous years there are some very interesting and unique tours taking place. Make sure you don’t miss out on these by checking booking details online and booking your place. Tours that need pre booking include: Medieval City Wall; BBC Coventry and Warwickshire Radio; Priory Undercroft and Visitor Centre; King Henry VIII School; Coventry Solihull Waste Disposal Ltd; and Coventry Central Library behind the scenes tour. A Heritage Open Days brochure will be available from Council buildings The Herbert Coventry Cathedral and libraries from Wednesday (23 August). For more details visit the website www.coventry.gov.uk/hod.
Hi thanks to be able to share here & be part of tnis group ; Good to kbow iof yt area as going down soon with Natiomal Trust centre from Glasgow
http://www.coventrytelegraph.net/whats-on/whats-on-news/future-coventrys-albany-theatre-been-13477790
https://www.facebook.com/peter.garbett.9/posts/1769150796433812 Sad we are loosing this museum visit it whilst you can
https://www.facebook.com/CoventryPVC/posts/859986554155927
Looking good in the sunshine.. https://www.facebook.com/visitcoventry.net/posts/501006916915750
Best unique Pubs Whats your favourite pubs and why? in Coventry and surrounding areas.
Thank you for letting me join your group
https://www.facebook.com/theprideofwillenhall/posts/1378187435629358
https://www.facebook.com/peter.garbett.9/posts/1740635729285319 Coventry shortlisted!
Nice. https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/489764101373365/?type=3
I ve always wanted to have a nose down this passageway.. https://www.facebook.com/visitcoventry.net/posts/487218148294627
http://www.coventrytelegraph.net/whats-on/music-nightlife-news/what-godiva-festival-weather-forecast-13275038
Looks like a great idea.
Lovely pic.. https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/483673398649102/?type=3
What is your favourite restaurant or eatery in Coventry area?
Interesting.. https://www.facebook.com/visitcoventry.net/photos/a.363722627310847.1073741828.361929627490147/481529385530170/?type=3
https://www.facebook.com/BerkswellWindmill/posts/1322847697832828
Just saw a post about people that made me think would almost be a great motto for our fabulous city. Let go of what s gone. Be grateful for (and save) what remains. Look forward to what is coming.
Why not make a weekend of it? It s Jaguar Super Saturday! In aid of Air Ambulance details on joining below if you own a Jaguar! This year there will be a timeline of cars from oldest to newest many built at Browns Lane!
:) we have a wonderful collection of exciting Citroen motorcars at the Museum on Sunday if you own a Citroen you re welcome to come along with your car 10-4pm it s FREE and you may be given a leaflet about the car club! Any Citroen can come along old or new thank you :)
City Wall Guided Costume Tour this Saturday meeting at 2pm at Priory Visitors Centre ( behind Nando s Trinity Street) with the Deep Fat Friar https://www.facebook.com/events/313502545772268/?ti=icl
https://www.facebook.com/BerkswellWindmill/posts/1322847697832828
If I want to do data science, where accessing data in a sensible way is key, then there must be a better way! There is.
4.1.2 Pandas
The Pandas library is designed with data science in mind. You will examine it more in the coming weeks. Reading CSV files with pandas is very easy.
import pandas as pd
The import x as y is good practice. In the below code, anything with a pd. prefix comes from pandas. This is particulary useful for preventing a module from overwriting inbuilt Python functionality.
= pd.read_csv('data/Facebook.csv', encoding = 'UTF-8') df
That was easy. We can even pick out specific rows.
'post_message'] df[
0 Don t forget to post your photographs from her...
1 If you want to learn about Historic Coventry t...
2 3 more dates for September Tours of The Mediev...
3 http://www.coventrytelegraph.net/whats-on/arts...
4 Different angle of St Michaels
5 The Heritage Open Days are taking place from 7...
6 Hi thanks to be able to share here & be part o...
7 http://www.coventrytelegraph.net/whats-on/what...
8 https://www.facebook.com/peter.garbett.9/posts...
9 https://www.facebook.com/CoventryPVC/posts/859...
10 Looking good in the sunshine.. https://www.fac...
11 Best unique Pubs Whats your favourite pubs and...
12 Thank you for letting me join your group
13 https://www.facebook.com/theprideofwillenhall/...
14 https://www.facebook.com/peter.garbett.9/posts...
15 Nice. https://www.facebook.com/visitcoventry.n...
16 I ve always wanted to have a nose down this pa...
17 http://www.coventrytelegraph.net/whats-on/musi...
18 Looks like a great idea.
19 Lovely pic.. https://www.facebook.com/visitcov...
20 What is your favourite restaurant or eatery in...
21 Interesting.. https://www.facebook.com/visitco...
22 https://www.facebook.com/BerkswellWindmill/pos...
23 Just saw a post about people that made me thin...
24 Why not make a weekend of it? It s Jaguar Supe...
25 :) we have a wonderful collection of exciting ...
26 City Wall Guided Costume Tour this Saturday me...
27 https://www.facebook.com/BerkswellWindmill/pos...
Name: post_message, dtype: object
And even look at the dataset in a pretty way.
df
type | post_message | link | link_domain | post_published | likes_count_fb | comments_count_fb | reactions_count_fb | shares_count_fb | engagement_fb | |
---|---|---|---|---|---|---|---|---|---|---|
0 | status | Don t forget to post your photographs from her... | NaN | NaN | 2017-09-09T13:57:14+0000 | 5 | 1 | 5 | 0 | 6 |
1 | status | If you want to learn about Historic Coventry t... | NaN | NaN | 2017-08-31T17:36:47+0000 | 7 | 12 | 7 | 0 | 19 |
2 | event | 3 more dates for September Tours of The Mediev... | https://www.facebook.com/events/1935828209971181/ | facebook.com | 2017-09-05T10:46:45+0000 | 2 | 0 | 2 | 0 | 2 |
3 | link | http://www.coventrytelegraph.net/whats-on/arts... | http://www.coventrytelegraph.net/whats-on/arts... | coventrytelegraph.net | 2017-08-14T08:56:04+0000 | 9 | 2 | 10 | 4 | 16 |
4 | status | Different angle of St Michaels | NaN | NaN | 2017-08-26T09:04:35+0000 | 11 | 0 | 12 | 0 | 12 |
5 | link | The Heritage Open Days are taking place from 7... | http://www.coventry.gov.uk/hod | coventry.gov.uk | 2017-08-21T10:26:52+0000 | 10 | 0 | 10 | 13 | 23 |
6 | status | Hi thanks to be able to share here & be part o... | NaN | NaN | 2017-08-17T08:36:07+0000 | 1 | 8 | 1 | 0 | 9 |
7 | link | http://www.coventrytelegraph.net/whats-on/what... | http://www.coventrytelegraph.net/whats-on/what... | coventrytelegraph.net | 2017-08-17T09:14:59+0000 | 8 | 1 | 9 | 1 | 11 |
8 | photo | https://www.facebook.com/peter.garbett.9/posts... | https://www.facebook.com/photo.php?fbid=128017... | facebook.com | 2017-08-10T11:49:47+0000 | 2 | 1 | 2 | 0 | 3 |
9 | link | https://www.facebook.com/CoventryPVC/posts/859... | http://www.crowdfunder.co.uk/coventry-priory | crowdfunder.co.uk | 2017-08-07T21:13:46+0000 | 6 | 1 | 6 | 0 | 7 |
10 | photo | Looking good in the sunshine.. https://www.fac... | https://www.facebook.com/visitcoventry.net/pho... | facebook.com | 2017-08-06T21:25:32+0000 | 8 | 0 | 8 | 1 | 9 |
11 | status | Best unique Pubs Whats your favourite pubs and... | NaN | NaN | 2017-07-19T08:35:49+0000 | 0 | 7 | 0 | 0 | 7 |
12 | status | Thank you for letting me join your group | NaN | NaN | 2017-07-19T14:30:58+0000 | 1 | 1 | 1 | 0 | 2 |
13 | link | https://www.facebook.com/theprideofwillenhall/... | http://www.coventrytelegraph.net/whats-on/what... | coventrytelegraph.net | 2017-07-19T08:23:55+0000 | 0 | 0 | 0 | 0 | 0 |
14 | link | https://www.facebook.com/peter.garbett.9/posts... | https://coventry2021.co.uk/news/?is_mobile=1 | coventry2021.co.uk | 2017-07-15T18:04:34+0000 | 2 | 0 | 2 | 0 | 2 |
15 | photo | Nice. https://www.facebook.com/visitcoventry.n... | https://www.facebook.com/visitcoventry.net/pho... | facebook.com | 2017-07-13T22:06:20+0000 | 3 | 0 | 3 | 0 | 3 |
16 | photo | I ve always wanted to have a nose down this pa... | https://www.facebook.com/visitcoventry.net/pho... | facebook.com | 2017-07-09T12:21:01+0000 | 11 | 1 | 11 | 0 | 12 |
17 | link | http://www.coventrytelegraph.net/whats-on/musi... | http://www.coventrytelegraph.net/whats-on/musi... | coventrytelegraph.net | 2017-07-08T08:39:38+0000 | 1 | 0 | 1 | 0 | 1 |
18 | video | Looks like a great idea. | https://www.facebook.com/stmarysguildhall/vide... | facebook.com | 2017-07-07T15:28:24+0000 | 7 | 0 | 9 | 2 | 11 |
19 | photo | Lovely pic.. https://www.facebook.com/visitcov... | https://www.facebook.com/visitcoventry.net/pho... | facebook.com | 2017-07-02T08:41:08+0000 | 15 | 2 | 16 | 1 | 19 |
20 | status | What is your favourite restaurant or eatery in... | NaN | NaN | 2017-06-29T08:55:29+0000 | 3 | 35 | 3 | 0 | 38 |
21 | photo | Interesting.. https://www.facebook.com/visitco... | https://www.facebook.com/visitcoventry.net/pho... | facebook.com | 2017-06-29T05:32:53+0000 | 9 | 0 | 9 | 0 | 9 |
22 | link | https://www.facebook.com/BerkswellWindmill/pos... | https://www.facebook.com/BerkswellWindmill/pos... | facebook.com | 2017-06-26T18:24:47+0000 | 0 | 0 | 0 | 0 | 0 |
23 | status | Just saw a post about people that made me thin... | NaN | NaN | 2017-06-25T17:17:12+0000 | 4 | 2 | 5 | 0 | 7 |
24 | event | Why not make a weekend of it? It s Jaguar Supe... | https://www.facebook.com/events/201361447045208/ | facebook.com | 2017-06-22T19:27:54+0000 | 2 | 0 | 2 | 0 | 2 |
25 | event | :) we have a wonderful collection of exciting ... | https://www.facebook.com/events/362084697478113/ | facebook.com | 2017-06-22T19:23:49+0000 | 1 | 0 | 1 | 0 | 1 |
26 | event | City Wall Guided Costume Tour this Saturday me... | https://www.facebook.com/events/313502545772268/ | facebook.com | 2017-06-22T19:20:55+0000 | 6 | 0 | 6 | 1 | 7 |
27 | link | https://www.facebook.com/BerkswellWindmill/pos... | https://www.facebook.com/BerkswellWindmill/pos... | facebook.com | 2017-06-22T16:59:56+0000 | 1 | 0 | 1 | 0 | 1 |
Huzzah!
A final point, pd.read_csv returns a pandas specific object with associated methods.
type(df)
pandas.core.frame.DataFrame
df.dtypes
type object
post_message object
link object
link_domain object
post_published object
likes_count_fb int64
comments_count_fb int64
reactions_count_fb int64
shares_count_fb int64
engagement_fb int64
dtype: object
Where int64 are integers and object here refers to strings.