You are on page 1of 1

Importer les bibliothèques numpy et pandas et importer functools de reduce

In [1]: # Importer les bibliothèques nécessaires


import numpy as np
import pandas as pd
from functools import reduce

PART I
Explorer la dataset BL-Flickr-Images-Book.csv

NB: faites attentionsau chemin

1 Exploration du dataset
In [2]: # Exporter BL-Flickr-Images-Book.csv dans un data frame nomé df
df = pd.read_csv('BL-Flickr-Images-Book.csv')
df.head()

# Afficher les 5 premières lignes

Out[2]: Edition Place of Date of Corporate Corporate Former Issuance


Identifier Publisher Title Author Contributors Engraver Flickr URL Shelfmarks
Statement Publication Publication Author Contributors owner type

Walter Forbes. British Library


S. Tinsley FORBES,
0 206 NaN London 1879 [1878] [A novel.] By A. A. A. NaN NaN NaN NaN monographic http://www.flickr.com/photos/britishlibrary/ta... HMNTS
& Co. Walter.
A 12641.b.30.

All for Greed. [A BLAZE DE


London; British Library
Virtue & novel. The A., A. BURY, Marie
1 216 NaN Virtue & 1868 NaN NaN NaN NaN monographic http://www.flickr.com/photos/britishlibrary/ta... HMNTS
Co. dedication A. Pauline Rose -
Yorston 12626.cc.2.
signed... Baroness

Love the BLAZE DE


Bradbury, British Library
Avenger. By the A., A. BURY, Marie
2 218 NaN London 1869 Evans & NaN NaN NaN NaN monographic http://www.flickr.com/photos/britishlibrary/ta... HMNTS
author of “All for A. Pauline Rose -
Co. 12625.dd.1.
Gr... Baroness

Welsh
Sketches, British Library
James A., E. Appleyard,
3 472 NaN London 1851 chiefly NaN NaN NaN NaN monographic http://www.flickr.com/photos/britishlibrary/ta... HMNTS
Darling S. Ernest Silvanus.
ecclesiastical, 10369.bbb.15.
to the...

A new
Wertheim [The World in British Library
edition, A., E. BROOME, John
4 480 London 1857 & which I live, and NaN NaN NaN NaN monographic http://www.flickr.com/photos/britishlibrary/ta... HMNTS
revised, S. Henry.
Macintosh my place in it... 9007.d.28.
etc.

2 Jeter un coup d'oeil dans le dataset


In [3]: df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8287 entries, 0 to 8286
Data columns (total 15 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Identifier 8287 non-null int64
1 Edition Statement 773 non-null object
2 Place of Publication 8287 non-null object
3 Date of Publication 8106 non-null object
4 Publisher 4092 non-null object
5 Title 8287 non-null object
6 Author 6509 non-null object
7 Contributors 8287 non-null object
8 Corporate Author 0 non-null float64
9 Corporate Contributors 0 non-null float64
10 Former owner 1 non-null object
11 Engraver 0 non-null float64
12 Issuance type 8287 non-null object
13 Flickr URL 8287 non-null object
14 Shelfmarks 8287 non-null object
dtypes: float64(3), int64(1), object(11)
memory usage: 971.3+ KB

3 Qu'est ce que vous remarquez?


In [25]: # Rédiger votre réponse

4 Dropping unnecessary columns


Nous allons restreindre l'étude à seulement 7 colomnes pour cela nous allons supprimer 7 colonnes que nous jugeons unitiles por le moment :

to_drop = ['Edition Statement',


'Corporate Author',
'Corporate Contributors',
'Former owner',
'Engraver',
'Contributors',
'Issuance type',
'Shelfmarks']```

In [5]: to_drop = ['Edition Statement',


'Corporate Author',
'Corporate Contributors',
'Former owner',
'Engraver',
'Contributors',
'Issuance type',
'Shelfmarks']

#Eliminer les colonnes dans la liste de to_drop du dataframe df


df.drop(to_drop, inplace = True, axis =1)

#Afficherle résultat
df.head()

Out[5]: Identifier Place of Publication Date of Publication Publisher Title Author Flickr URL

0 206 London 1879 [1878] S. Tinsley & Co. Walter Forbes. [A novel.] By A. A A. A. http://www.flickr.com/photos/britishlibrary/ta...

1 216 London; Virtue & Yorston 1868 Virtue & Co. All for Greed. [A novel. The dedication signed... A., A. A. http://www.flickr.com/photos/britishlibrary/ta...

2 218 London 1869 Bradbury, Evans & Co. Love the Avenger. By the author of “All for Gr... A., A. A. http://www.flickr.com/photos/britishlibrary/ta...

3 472 London 1851 James Darling Welsh Sketches, chiefly ecclesiastical, to the... A., E. S. http://www.flickr.com/photos/britishlibrary/ta...

4 480 London 1857 Wertheim & Macintosh [The World in which I live, and my place in it... A., E. S. http://www.flickr.com/photos/britishlibrary/ta...

5 Setting the index of the dataset


Changer l'index pourqu'ille prenne la valeur de l'identifiant Identifier

In [8]: ###Changerl'index
df.set_index('Identifier',inplace = True)

###Afficher le résultat
df.head(50)

Out[8]: Date of
Place of Publication Publisher Title Author Flickr URL
Publication

Identifier

206 London 1879 [1878] S. Tinsley & Co. Walter Forbes. [A novel.] By A. A A. A. http://www.flickr.com/photos/britishlibrary/ta...

All for Greed. [A novel. The dedication


216 London; Virtue & Yorston 1868 Virtue & Co. A., A. A. http://www.flickr.com/photos/britishlibrary/ta...
signed...

Love the Avenger. By the author of “All for


218 London 1869 Bradbury, Evans & Co. A., A. A. http://www.flickr.com/photos/britishlibrary/ta...
Gr...

Welsh Sketches, chiefly ecclesiastical, to


472 London 1851 James Darling A., E. S. http://www.flickr.com/photos/britishlibrary/ta...
the...

480 London 1857 Wertheim & Macintosh [The World in which I live, and my place in it... A., E. S. http://www.flickr.com/photos/britishlibrary/ta...

481 London 1875 William Macintosh [The World in which I live, and my place in it... A., E. S. http://www.flickr.com/photos/britishlibrary/ta...

Lagonells. By the author of Darmayne (F. E.


519 London 1872 The Author A., F. E. http://www.flickr.com/photos/britishlibrary/ta...
A....

pp. 40. G. Bryan & Co: Oxford, The Coming of Spring, and other poems. By
667 NaN NaN A., J.|A., J. http://www.flickr.com/photos/britishlibrary/ta...
1898 J. A...

A Warning to the inhabitants of England, and


874 London] 1676 NaN Remaʿ. http://www.flickr.com/photos/britishlibrary/ta...
L...

A Satyr against Vertue. (A poem: supposed to


1143 London 1679 NaN A., T. http://www.flickr.com/photos/britishlibrary/ta...
b...

An Account of the many and great Loans,


1280 Coventry 1802 Printed by J. Turner NaN http://www.flickr.com/photos/britishlibrary/ta...
Benefa...

Erindringer som Bidrag til Norges Historie


1808 Christiania 1859 NaN AALL, Jacob. http://www.flickr.com/photos/britishlibrary/ta...
fra...

AAR, Ermanno - pseud. [i.e. Luigi Giuseppe


1905 Firenze 1888 NaN Gli Studi storici in terra d'Otranto ... Framm... http://www.flickr.com/photos/britishlibrary/ta...
Oro...

De Aardbol. Magazijn van hedendaagsche


1929 Amsterdam 1839, 38-54 NaN NaN http://www.flickr.com/photos/britishlibrary/ta...
land- e...

Cronache Savonesi dal 1500 al 1570 ...


2836 Savona 1897 NaN ABATE, Giovanni Agostino. http://www.flickr.com/photos/britishlibrary/ta...
Accresc...

2854 London 1865 E. Moxon & Co. See-Saw; a novel ... Edited [or rather, writte... ABATI, Francesco. http://www.flickr.com/photos/britishlibrary/ta...

2956 Paris 1860-63 NaN Géodésie d'une partie de la Haute Éthiopie,... ABBADIE, Antoine Thompson d'. http://www.flickr.com/photos/britishlibrary/ta...

2957 Paris 1873 NaN [With eleven maps.] ABBADIE, Antoine Thompson d'. http://www.flickr.com/photos/britishlibrary/ta...

ABBAD Y LASIERRA, Agustín Íñigo -


3017 Puerto-Rico 1866 NaN [Historia geográfica, civil y politica de la ... http://www.flickr.com/photos/britishlibrary/ta...
Bishop of...

The Crisis of the Revolution, being the story


3131 New York 1899 W. Abbatt ABBATT, William. http://www.flickr.com/photos/britishlibrary/ta...
...

Peace: a lyric poem. [With prefatory address


4598 Hull 1814 The Author ABBOTT, Thomas Eastoe. http://www.flickr.com/photos/britishlibrary/ta...
b...

Abdallah; or, The Arabian Martyr: a Christian


4884 London 1820 J. Hatchard & Son NaN http://www.flickr.com/photos/britishlibrary/ta...
...

[Abdollatiphi Historiæ Ægypti compendium.


4976 Oxonii 1800 J. Cooke, etc. NaN http://www.flickr.com/photos/britishlibrary/ta...
[Wit...

The Comic History of England ... With ...


5382 London 1847, 48 [1846-48] Punch Office A'BECKETT, Gilbert Abbott. http://www.flickr.com/photos/britishlibrary/ta...
colo...

[The comic history of England ... With twenty


5385 London [1897?] Bradbury, Agnew & Co. A'BECKETT, Gilbert Abbott. http://www.flickr.com/photos/britishlibrary/ta...
...

[The Comic History of Rome ... Illustrated by


5389 London [1897?] Bradbury, Agnew & Co. A'BECKETT, Gilbert Abbott. http://www.flickr.com/photos/britishlibrary/ta...
...

Signa: opera in tre atti [founded on the A'BECKETT, Gilbert Arthur - and RUDALL
5432 Milano 1893 NaN http://www.flickr.com/photos/britishlibrary/ta...
novel... (H. A.)

The Venetian Outlaw, a drama in three acts


6036 London 1805 C. & R. Baldwin NaN http://www.flickr.com/photos/britishlibrary/ta...
......

Description of the Coast between Aberdeen


6821 Aberdeen 1837 J. Davidson & Co. NaN http://www.flickr.com/photos/britishlibrary/ta...
and ...

Aus Kaukasischen Ländern. Reisebriefe von


7521 Wien 1896 NaN ABICH, Wilhelm Hermann. http://www.flickr.com/photos/britishlibrary/ta...
H. ...

Selections from the Municipal Chronicles of


7630 Abingdon 1898 W. H. Hooke NaN http://www.flickr.com/photos/britishlibrary/ta...
th...

The Plains of Abraham, 1759, a spot sacred


8239 Quebec 1899 L. J. Demers & Frère NaN http://www.flickr.com/photos/britishlibrary/ta...
to ...

The Witwatersrand Gold Mines. A true and


8435 London 1892 Effingham Wilson & Co. ABRAHAM, Félix - Writer on Mining http://www.flickr.com/photos/britishlibrary/ta...
unvar...

The New Era of the Goldmining Industry in


8440 London 1894 Effingham Wilson ABRAHAM, Félix - Writer on Mining http://www.flickr.com/photos/britishlibrary/ta...
the ...

11361 Leipzig [1894-96 NaN Tirol und Vorarlberg ... Mit ... einer Karte, ... ACHLEITNER, Arthur - and UBL (E.) http://www.flickr.com/photos/britishlibrary/ta...

pp. 40. W. Cann: Plymouth, A Question of Holy Writ, suggested by a


11852 NaN NaN ACKLAND - Lieut., R.N http://www.flickr.com/photos/britishlibrary/ta...
[1876?] lectur...

A Summer in High Asia: being a record of


13074 London 1899 W. Thacker & Co. ADAIR, Frederick Edward Shafto. http://www.flickr.com/photos/britishlibrary/ta...
sport...

A Hand-book and History of Sidmouth from


13364 London [1885] Griffith, Farran & Co. NaN http://www.flickr.com/photos/britishlibrary/ta...
the T...

The Historie of Eald Street, now called Old


14466 London [1860?] Adams & King NaN http://www.flickr.com/photos/britishlibrary/ta...
St...

First (-Third) Annual Report on the Geology


14703 1845 1846 NaN NaN http://www.flickr.com/photos/britishlibrary/ta...
of...

The New Egypt. A social sketch. [Edited by J.


15141 London 1893 T. Fisher Unwin ADAMS, Francis William Lauderdale. http://www.flickr.com/photos/britishlibrary/ta...
...

Tiberius: a drama ... With introduction by W.


15146 London 1894 T. Fisher Unwin ADAMS, Francis William Lauderdale. http://www.flickr.com/photos/britishlibrary/ta...
...

printed by W. Thorne, and published by J. Memoirs of the Life and Doctrines of the late
16173 enk 1817 ADAMS, Joseph - M.D., F.L.S http://www.flickr.com/photos/britishlibrary/ta...
Call... ...

The Narrative of Robert Adams, a sailor, who


16543 London 1816 John Murray Adams, Robert - Sailor http://www.flickr.com/photos/britishlibrary/ta...
w...

[The Narrative of Robert Adams, a sailor, who


16544 Boston 1817 Wells & Lilley ADAMS, Robert - Sailor http://www.flickr.com/photos/britishlibrary/ta...
...

17602 London 1897 J. Bowden East End Idylls ... With an introduction by th... Adcock, Arthur St. John http://www.flickr.com/photos/britishlibrary/ta...

17752 Upsaliæ [1833] NaN Acta historiam regis Chistierni II. illustrant... ADDE, Petrus Adolphus. http://www.flickr.com/photos/britishlibrary/ta...

[The Works of the late Right Honourable


18173 London 1804 Vernor & Hood ADDISON, Joseph - Right Hon http://www.flickr.com/photos/britishlibrary/ta...
Joseph...

The Poetical Works of Joseph Addison; Gay's


18218 Edinburgh 1859 James Nichol Addison, Joseph http://www.flickr.com/photos/britishlibrary/ta...
Fa...

L'Esprit d'Addisson, ou les Beautés du


18347 Yverdon 1777 NaN ADDISON, Joseph - Right Hon http://www.flickr.com/photos/britishlibrary/ta...
Specta...

6 Donner un appérçu sur la colonne Date of Publication


In [9]: ### Afficher les 25 premières valeurs de Date of Publication
df['Date of Publication'].head(25)

Out[9]: Identifier
206 1879 [1878]
216 1868
218 1869
472 1851
480 1857
481 1875
519 1872
667 NaN
874 1676
1143 1679
1280 1802
1808 1859
1905 1888
1929 1839, 38-54
2836 1897
2854 1865
2956 1860-63
2957 1873
3017 1866
3131 1899
4598 1814
4884 1820
4976 1800
5382 1847, 48 [1846-48]
5385 [1897?]
Name: Date of Publication, dtype: object

Remarque : Observer la date de publication qui correspond à l'index 206. Ca nécessite un nettoyage et une correction : il faut éliminer certains symboles

7 Cleaning columns using the .apply function

Ecrire une fonction qui permet de nettoyer la colonne des dates


In [10]: unwanted_characters = ['[', ',', '-']
# Completer la fonction suivante
def clean_dates(item):
dop= str(item.loc['Date of Publication'])

if dop == 'nan' or dop[0] == '[':


pass

for character in unwanted_characters:


if character in dop:
pass
pass

return dop

df['Date of Publication'] = df.apply(clean_dates, axis = 1)

In [13]: df.head()

Out[13]: Place of Publication Date of Publication Publisher Title Author Flickr URL

Identifier

206 London 1879 [1878] S. Tinsley & Co. Walter Forbes. [A novel.] By A. A A. A. http://www.flickr.com/photos/britishlibrary/ta...

216 London; Virtue & Yorston 1868 Virtue & Co. All for Greed. [A novel. The dedication signed... A., A. A. http://www.flickr.com/photos/britishlibrary/ta...

218 London 1869 Bradbury, Evans & Co. Love the Avenger. By the author of “All for Gr... A., A. A. http://www.flickr.com/photos/britishlibrary/ta...

472 London 1851 James Darling Welsh Sketches, chiefly ecclesiastical, to the... A., E. S. http://www.flickr.com/photos/britishlibrary/ta...

480 London 1857 Wertheim & Macintosh [The World in which I live, and my place in it... A., E. S. http://www.flickr.com/photos/britishlibrary/ta...

In [14]: #Voici une autre méthode


#exécuter pour voir le résultat
unwanted_characters = ['[', ',', '-']

def clean_dates(dop):
dop = str(dop)
if dop.startswith('[') or dop == 'nan':
return 'NaN'
for character in unwanted_characters:
if character in dop:
character_index = dop.find(character)
dop = dop[:character_index]
return dop

df['Date of Publication'] = df['Date of Publication'].apply(clean_dates)


df.head(25)

Out[14]: Place of Publication Date of Publication Publisher Title Author Flickr URL

Identifier

206 London 1879 S. Tinsley & Co. Walter Forbes. [A novel.] By A. A A. A. http://www.flickr.com/photos/britishlibrary/ta...

216 London; Virtue & Yorston 1868 Virtue & Co. All for Greed. [A novel. The dedication signed... A., A. A. http://www.flickr.com/photos/britishlibrary/ta...

218 London 1869 Bradbury, Evans & Co. Love the Avenger. By the author of “All for Gr... A., A. A. http://www.flickr.com/photos/britishlibrary/ta...

472 London 1851 James Darling Welsh Sketches, chiefly ecclesiastical, to the... A., E. S. http://www.flickr.com/photos/britishlibrary/ta...

480 London 1857 Wertheim & Macintosh [The World in which I live, and my place in it... A., E. S. http://www.flickr.com/photos/britishlibrary/ta...

481 London 1875 William Macintosh [The World in which I live, and my place in it... A., E. S. http://www.flickr.com/photos/britishlibrary/ta...

519 London 1872 The Author Lagonells. By the author of Darmayne (F. E. A.... A., F. E. http://www.flickr.com/photos/britishlibrary/ta...

667 pp. 40. G. Bryan & Co: Oxford, 1898 NaN NaN The Coming of Spring, and other poems. By J. A... A., J.|A., J. http://www.flickr.com/photos/britishlibrary/ta...

874 London] 1676 NaN A Warning to the inhabitants of England, and L... Remaʿ. http://www.flickr.com/photos/britishlibrary/ta...

1143 London 1679 NaN A Satyr against Vertue. (A poem: supposed to b... A., T. http://www.flickr.com/photos/britishlibrary/ta...

1280 Coventry 1802 Printed by J. Turner An Account of the many and great Loans, Benefa... NaN http://www.flickr.com/photos/britishlibrary/ta...

1808 Christiania 1859 NaN Erindringer som Bidrag til Norges Historie fra... AALL, Jacob. http://www.flickr.com/photos/britishlibrary/ta...

1905 Firenze 1888 NaN Gli Studi storici in terra d'Otranto ... Framm... AAR, Ermanno - pseud. [i.e. Luigi Giuseppe Oro... http://www.flickr.com/photos/britishlibrary/ta...

1929 Amsterdam 1839 NaN De Aardbol. Magazijn van hedendaagsche land- e... NaN http://www.flickr.com/photos/britishlibrary/ta...

2836 Savona 1897 NaN Cronache Savonesi dal 1500 al 1570 ... Accresc... ABATE, Giovanni Agostino. http://www.flickr.com/photos/britishlibrary/ta...

2854 London 1865 E. Moxon & Co. See-Saw; a novel ... Edited [or rather, writte... ABATI, Francesco. http://www.flickr.com/photos/britishlibrary/ta...

2956 Paris 1860 NaN Géodésie d'une partie de la Haute Éthiopie,... ABBADIE, Antoine Thompson d'. http://www.flickr.com/photos/britishlibrary/ta...

2957 Paris 1873 NaN [With eleven maps.] ABBADIE, Antoine Thompson d'. http://www.flickr.com/photos/britishlibrary/ta...

3017 Puerto-Rico 1866 NaN [Historia geográfica, civil y politica de la ... ABBAD Y LASIERRA, Agustín Íñigo - Bishop of... http://www.flickr.com/photos/britishlibrary/ta...

3131 New York 1899 W. Abbatt The Crisis of the Revolution, being the story ... ABBATT, William. http://www.flickr.com/photos/britishlibrary/ta...

4598 Hull 1814 The Author Peace: a lyric poem. [With prefatory address b... ABBOTT, Thomas Eastoe. http://www.flickr.com/photos/britishlibrary/ta...

4884 London 1820 J. Hatchard & Son Abdallah; or, The Arabian Martyr: a Christian ... NaN http://www.flickr.com/photos/britishlibrary/ta...

4976 Oxonii 1800 J. Cooke, etc. [Abdollatiphi Historiæ Ægypti compendium. [Wit... NaN http://www.flickr.com/photos/britishlibrary/ta...

5382 London 1847 Punch Office The Comic History of England ... With ... colo... A'BECKETT, Gilbert Abbott. http://www.flickr.com/photos/britishlibrary/ta...

5385 London NaN Bradbury, Agnew & Co. [The comic history of England ... With twenty ... A'BECKETT, Gilbert Abbott. http://www.flickr.com/photos/britishlibrary/ta...

8 Observer la collone Author

Completer la fonction clean_author_names qui permet de corriger les valeurs de cet colonne

In [15]: # compléter la fonction suivante ( à la place de pass)


def clean_author_names(author):

author = str(author)

if author == 'nan':
return 'NaN'

author = author.split(',')

if len(author) == 1:
name = filter(lambda x: x.isalpha(), author[0])
return reduce(lambda x, y: x + y, name)

last_name, first_name = author[0], author[1]

first_name = first_name[:first_name.find('-')] if '-' in first_name else first_name

if first_name.endswith(('.', '.|')):
parts = first_name.split(',')

if len(parts) > 1:
first_occurence = first_name.find(',')
final_occurence = first_name.find(',', first_occurence + 1)
first_name = first_name[:final_occurence]
else:
first_name = first_name[:first_name.find(',')]

last_name = last_name.capitalize()

return f'{first_name} {last_name}'

df['Author'] = df['Author'].apply(clean_author_names)

Résultat attendu

In [16]: df.head()

Out[16]: Place of Publication Date of Publication Publisher Title Author Flickr URL

Identifier

206 London 1879 S. Tinsley & Co. Walter Forbes. [A novel.] By A. A AA http://www.flickr.com/photos/britishlibrary/ta...

216 London; Virtue & Yorston 1868 Virtue & Co. All for Greed. [A novel. The dedication signed... A. A A. http://www.flickr.com/photos/britishlibrary/ta...

218 London 1869 Bradbury, Evans & Co. Love the Avenger. By the author of “All for Gr... A. A A. http://www.flickr.com/photos/britishlibrary/ta...

472 London 1851 James Darling Welsh Sketches, chiefly ecclesiastical, to the... E. S A. http://www.flickr.com/photos/britishlibrary/ta...

480 London 1857 Wertheim & Macintosh [The World in which I live, and my place in it... E. S A. http://www.flickr.com/photos/britishlibrary/ta...

Observer la colone title

9 Completer la fonction clean_title qui permet de corriger les valeurs de cet colonne

Résultat attendu

In [12]: ## complétrer la fonction suivante ( à la place de pass)

def clean_title(title):

if title == 'nan':
return 'NaN'

if title[0] == '[':
pass

if 'by' in title:
pass
elif 'By' in title:
pass

if '[' in title:
pass

title = title[:-2]

title = list(map(str.capitalize, title.split()))


return ' '.join(title)

df['Title'] = df['Title'].apply(clean_title)
df.head()

Out[12]: Place of Publication Date of Publication Publisher Title Author Flickr URL

Identifier

206 London 1879 S. Tinsley & Co. Walter Forbes AA http://www.flickr.com/photos/britishlibrary/ta...

216 London; Virtue & Yorston 1868 Virtue & Co. All For Greed A. A A. http://www.flickr.com/photos/britishlibrary/ta...

218 London 1869 Bradbury, Evans & Co. Love The Avenger A. A A. http://www.flickr.com/photos/britishlibrary/ta...

472 London 1851 James Darling Welsh Sketches, Chiefly Ecclesiastical, To The... E. S A. http://www.flickr.com/photos/britishlibrary/ta...

480 London 1857 Wertheim & Macintosh The World In Which I Live, And My Place In It E. S A. http://www.flickr.com/photos/britishlibrary/ta...

PART II
1 Cleaning entire dataset
On considère le fichier suivant 'university_towns.txt'
In [17]: !head Datasets/university_towns.txt

'head' n’est pas reconnu en tant que commande interne


ou externe, un programme exécutable ou un fichier de commandes.

In [13]: with open('Datasets/university_towns.txt', 'r') as text:


textfile = text.read()
print(textfile)

Alabama[edit]
Auburn (Auburn University)[1]
Florence (University of North Alabama)
Jacksonville (Jacksonville State University)[2]
Livingston (University of West Alabama)[2]
Montevallo (University of Montevallo)[2]
Troy (Troy University)[2]
Tuscaloosa (University of Alabama, Stillman College, Shelton State)[3][4]
Tuskegee (Tuskegee University)[5]
Alaska[edit]
Fairbanks (University of Alaska Fairbanks)[2]
Arizona[edit]
Flagstaff (Northern Arizona University)[6]
Tempe (Arizona State University)
Tucson (University of Arizona)
Arkansas[edit]
Arkadelphia (Henderson State University, Ouachita Baptist University)[2]
Conway (Central Baptist College, Hendrix College, University of Central Arkansas)[2]
Fayetteville (University of Arkansas)[7]
Jonesboro (Arkansas State University)[8]
Magnolia (Southern Arkansas University)[2]
Monticello (University of Arkansas at Monticello)[2]
Russellville (Arkansas Tech University)[2]
Searcy (Harding University)[5]
California[edit]
Angwin (Pacific Union College)[2]
Arcata (Humboldt State University)[5]
Berkeley (University of California, Berkeley)[5]
Chico (California State University, Chico)[2]
Claremont (Claremont McKenna College, Pomona College, Harvey Mudd College, Scripps College, Pitzer College, Keck Graduate Institute, Claremont Graduate University)[5]
Cotati (California State University, Sonoma)[2]
Davis (University of California, Davis)[1]
Irvine (University of California, Irvine)
Isla Vista (University of California, Santa Barbara)[2]
University Park, Los Angeles (University of Southern California)
Merced (University of California, Merced)
Orange (Chapman University)
Palo Alto (Stanford University)
Pomona (Cal Poly Pomona, WesternU)[9][10][11] and formerly Pomona College
Redlands (University of Redlands)
Riverside (University of California, Riverside, California Baptist University, La Sierra University)
Sacramento (California State University, Sacramento)
University District, San Bernardino (California State University, San Bernardino, American Sports University)
San Diego (University of California, San Diego, San Diego State University)
San Luis Obispo (California Polytechnic State University)[2]
Santa Barbara (Fielding Graduate University, Santa Barbara City College, University of California, Santa Barbara, Westmont College)[2]
Santa Cruz (University of California, Santa Cruz)[2]
Turlock (California State University, Stanislaus)
Westwood, Los Angeles (University of California, Los Angeles)[2]
Whittier (Whittier CollegeRio Hondo College)
Colorado[edit]
Alamosa (Adams State College)[2]
Boulder (University of Colorado at Boulder)[12]
Durango (Fort Lewis College)[2]
Fort Collins (Colorado State University)[13]
Golden (Colorado School of Mines)
Grand Junction (Colorado Mesa University)
Greeley (University of Northern Colorado)
Gunnison (Western State College)[2]
Pueblo, Colorado (Colorado State University-Pueblo)
Connecticut[edit]
Fairfield (Fairfield University, Sacred Heart University)
Middletown (Wesleyan University)
New Britain (Central Connecticut State University)
New Haven (Yale University, University of New Haven, Southern Connecticut State University, Albertus Magnus College, Quinnipiac University)[14]
New London (Connecticut College, US Coast Guard Academy, Mitchell College)[2]
Storrs (University of Connecticut)[2]
Willimantic (Eastern Connecticut State University)[2]
Delaware[edit]
Dover (Delaware State University)[1]
Newark (University of Delaware)[1]
Florida[edit]
Ave Maria (Ave Maria University)
Boca Raton (Florida Atlantic University)
Coral Gables (University of Miami)
DeLand (Stetson University)[5]
Estero (Florida Gulf Coast University)
Gainesville (University of Florida, Santa Fe College)
Orlando (University of Central Florida)
Sarasota (New College of Florida, Ringling College of Art and Design, State College of Florida, Manatee-Sarasota, University of South Florida Sarasota-Manatee)
St. Augustine (Flagler College)
St. Leo (St. Leo University)
Tallahassee (Florida State University, Florida A&M University)
Tampa (University of South Florida)
Georgia[edit]
Albany (Albany State University)
Athens (University of Georgia)[15]
Atlanta (Georgia State University, Georgia Tech, Emory)[2]
Carrollton (University of West Georgia)[2]*Dahlonega (North Georgia College & State University)[2]
Demorest (Piedmont College)[2]
Fort Valley (Fort Valley State University)[2]
Kennesaw (Kennesaw State University)
Milledgeville (Georgia College & State University)[2]
Mount Vernon (Brewton-Parker College)[2]
Oxford (Oxford College)
Rome (Berry College, Shorter University)
Savannah (Armstrong Atlantic State University, Savannah State University, Savannah College of Art and Design)
Statesboro (Georgia Southern University)[5]
Valdosta (Valdosta State University)[2]
Waleska (Reinhardt College)[2]
Young Harris (Young Harris College)[2]
Hawaii[edit]
Manoa (University of Hawaii at Manoa)[2]
Idaho[edit]
Moscow (University of Idaho)[2]
Pocatello (Idaho State University)[2]
Rexburg (BYU-Idaho)[2]
Illinois[edit]
Carbondale (Southern Illinois University Carbondale)[5]
Champaign–Urbana (University of Illinois at Urbana–Champaign)[5]
Charleston (Eastern Illinois University)[2]
DeKalb (Northern Illinois University)[2]
Edwardsville (Southern Illinois University Edwardsville)[2]
Evanston (Northwestern University)[2]
Lebanon (McKendree University)[2]
Macomb (Western Illinois University)[2]
Normal (Illinois State University)[2]
Peoria (Bradley University)
Indiana[edit]
Bloomington (Indiana University Bloomington)[5]
Crawfordsville (Wabash College)
Greencastle (DePauw University)[5]
Hanover (Hanover College)[2]
Marion (Indiana Wesleyan University)[2]
Muncie (Ball State University)[2]
Oakland City (Oakland City University)[2]
Richmond (Earlham College)[2]
South Bend (Notre Dame University[2])
Terre Haute (Indiana State University, Rose-Hulman Institute of Technology)[2]
Upland (Taylor University)[2]
Valparaiso (Valparaiso University)
West Lafayette (Purdue University)[2]
Iowa[edit]
Ames (Iowa State University)[2]
Cedar Falls (University of Northern Iowa)[2]
Cedar Rapids, Iowa (Coe College )
Decorah (Luther College)[5]
Fayette (Upper Iowa University)[2]
Grinnell (Grinnell College)[15]
Iowa City (University of Iowa)[15]
Lamoni (Graceland University)[2]
Mount Vernon, (Cornell College)
Orange City (Northwestern College)[2]
Sioux Center (Dordt College)[2]
Storm Lake (Buena Vista University)[2]
Waverly (Wartburg College)[2]
Kansas[edit]
Baldwin City (Baker University)[5]
Emporia (Emporia State University)[2]
Hays (Fort Hays State University)[2]
Lawrence (University of Kansas, Haskell Indian Nations University)[15]
Manhattan (Kansas State University, Manhattan Christian College)[15]
Pittsburg (Pittsburg State University)[2]
Kentucky[edit]
Bowling Green (Western Kentucky University)[2]
Columbia (Lindsey Wilson College)[2]
Georgetown (Georgetown College)
Highland Heights (Northern Kentucky University)
Lexington (University of Kentucky, Transylvania University[5]
Louisville (University of Louisville)
Morehead (Morehead State University)[2]
Murray (Murray State University)[5]
Richmond (Eastern Kentucky University)[2]
Williamsburg (University of the Cumberlands)[2]
Wilmore (Asbury University, Asbury Theological Seminary)[2]
Louisiana[edit]
Baton Rouge (Louisiana State University, Southern University)
Grambling (Grambling State University)[5]
Hammond (Southeastern Louisiana University)[2]
Lafayette (University of Louisiana at Lafayette)
Monroe (University of Louisiana at Monroe)[2]
Natchitoches (Northwestern State University)[2]
Ruston (Louisiana Tech University)[2]
Thibodaux (Nicholls State University)[2]
Maine[edit]
Augusta (University of Maine at Augusta)[2]
Bar Harbor (College of the Atlantic)
Brunswick (Bowdoin College)
Farmington (University of Maine at Farmington)[2]
Fort Kent (University of Maine at Fort Kent)
Gorham (University of Southern Maine)[2]
Lewiston, Maine (Bates College)
Orono (University of Maine)[2]
Waterville (Thomas College, Colby College)
Maryland[edit]
Annapolis (United States Naval Academy, St. John's College)
Chestertown (Washington College)[2]
College Park (University of Maryland, College Park)[16]
Cumberland (Allegany College of Maryland)
Emmitsburg (Mount St. Mary's University)[2]
Frostburg (Frostburg State University)[5]
Princess Anne (University of Maryland Eastern Shore)[5]
Towson (Towson University, Goucher College)[2]
Salisbury (Salisbury University)[2]
Westminster (McDaniel College)
Massachusetts[edit]
Boston (Boston University, Boston College, Boston Conservatory, New England Conservatory, Brandeis University, Northeastern University, UMass Boston, Emmanuel College, Bunker
Hill Community College, Roxbury Community College, Suffolk University, Simmons College, among many others)
Bridgewater (Bridgewater State College)[2]
Cambridge (Harvard University, Massachusetts Institute of Technology)(Lesley University, Cambridge College, Longy School of Music)[15]
Chestnut Hill (Boston College)
The Colleges of Worcester Consortium:
Dudley (Nichols College)
North Grafton (Cummings School of Veterinary Medicine at Tufts University)
Paxton (Anna Maria College)
Worcester (Assumption, Becker, Clark University, Holy Cross, Mass. College of Pharmacy & Health Sciences, Quinsigamond Community College, UMass Medical School, Worcester State
University, Worcester Polytechnic Institute)
The Five College Region of Western Massachusetts:
Amherst (Amherst College, Hampshire College, University of Massachusetts Amherst)[15]
Northampton (Smith College)
South Hadley (Mount Holyoke College)
Fitchburg (Fitchburg State College)
North Adams (Massachusetts College of Liberal Arts)
Springfield (American International College), (Springfield College), and (Western New England College)
Waltham (Bentley University), (Brandeis University)
Williamstown (Williams College)
Framingham (Framingham State University)
Michigan[edit]
Adrian (Adrian College, Siena Heights University)
Albion (Albion College)[17]
Allendale (Grand Valley State University)
Alma (Alma College)
Ann Arbor (University of Michigan)[1]
Berrien Springs (Andrews University)[2]
Big Rapids (Ferris State University)[2]
East Lansing (Michigan State University)[2]
Flint (Kettering University, University of Michigan-Flint)
Hillsdale (Hillsdale College)
Houghton (Michigan Technological University)[5]
Kalamazoo (Western Michigan University, Kalamazoo College)[2]
Marquette (Northern Michigan University)[2]
Midland (Northwood University)
Mount Pleasant (Central Michigan University)[2]
Olivet (Olivet College)[2]
Saginaw (Saginaw Valley State University)
Sault Ste. Marie (Lake Superior State University)
Spring Arbor (Spring Arbor University)[2]
Ypsilanti (Eastern Michigan University)[2]
Minnesota[edit]
Bemidji (Bemidji State University)[2]
Crookston (University of Minnesota Crookston)[2]
Duluth (University of Minnesota Duluth, Lake Superior College, The College of St. Scholastica, University of Wisconsin–Superior, Duluth Business University
Faribault, South Central College
Mankato (Minnesota State University, Mankato),[2] Bethany Lutheran College
Marshall (Southwest Minnesota State University)[2]
Moorhead (Minnesota State University, Moorhead, Concordia College)[18]
Morris (University of Minnesota Morris)[2]
Northfield (Carleton College, St. Olaf College)[5]
North Mankato, South Central College
St. Cloud (St. Cloud State University, The College of St. Scholastica)[2]
St. Joseph (College of Saint Benedict)[2]
St. Peter (Gustavus Adolphus College)[2]
Winona (Winona State University, St. Mary's University of Minnesota)[19]
Mississippi[edit]
Cleveland (Delta State University)[2]
Hattiesburg (University of Southern Mississippi)[20]
Itta Bena (Mississippi Valley State University)[2]
Oxford (University of Mississippi)[2]
Starkville (Mississippi State University)[2]
Missouri[edit]
Bolivar (Southwest Baptist University)[2]
Cape Girardeau (Southeast Missouri State University)[2]
Columbia (University of Missouri, Stephens College, Columbia College)[20]
Fayette (Central Methodist University)[2]
Fulton (Westminster College and William Woods University).
Kirksville (Truman State University, A. T. Still University)[2]
Maryville (Northwest Missouri State University)[2]
Rolla (Missouri University of Science and Technology)[2]
Warrensburg (University of Central Missouri)[5]
Montana[edit]
Bozeman (Montana State University)[2]
Dillon (University of Montana Western)[2]
Missoula (University of Montana)[5]
Nebraska[edit]
Chadron (Chadron State College)[5]
Crete (Doane College)[2]
Kearney (University of Nebraska at Kearney)[2]
Lincoln (University of Nebraska at Lincoln)[5]
Peru (Peru State College)[2]
Seward (Concordia University)[2]
Wayne (Wayne State College)[2]
Nevada[edit]
Las Vegas (University of Nevada, Las Vegas)
Reno (University of Nevada, Reno)
New Hampshire[edit]
New London, New Hampshire (Colby-Sawyer College)
Durham (University of New Hampshire)[2]
Hanover (Dartmouth College)[5]
Henniker (New England College)
Keene (Keene State College)[2]
Plymouth (Plymouth State University)[2]
Rindge (Franklin Pierce University)
New Jersey[edit]
Ewing (The College of New Jersey), (Rider University)
Jersey City (New Jersey City University), (Saint Peter's University)
Glassboro (Rowan University)[2]
Hoboken (Stevens Institute of Technology)
Madison (Drew University), (Fairleigh Dickinson University), (College of Saint Elizabeth)
Newark (Rutgers University), (New Jersey Institute of Technology), (UMDNJ)
New Brunswick (Rutgers University)[5]
Princeton (Princeton University)[5]
Union (Kean University)
West Long Branch (Monmouth University)
New Mexico[edit]
Hobbs (University of the Southwest)[2]
Las Cruces (New Mexico State University)[2]
Las Vegas (New Mexico Highlands University)[2]
Portales (Eastern New Mexico University)[2]
Silver City (Western New Mexico University)[2]
New York[edit]
Alfred (Alfred University, Alfred State College)[2]
Albany (SUNY Albany, Siena College, Albany College of Pharmacy, Albany Law School, Albany Medical College, College of Saint Rose, Excelsior College, Maria College of Albany, M
ildred Elley, Sage College of Albany)
Aurora (Wells College)[21]
Binghamton (Binghamton University)[2]
Brockport (SUNY Brockport)[5]
Buffalo (University at Buffalo)
Canton (St. Lawrence University, SUNY Canton)[2]
Clinton (Hamilton College)[2]
Cobleskill (SUNY Cobleskill)[2]
Delhi (SUNY Delhi)[2]
Fredonia (SUNY Fredonia)[2]
Geneseo (SUNY Geneseo)[2]
Geneva (Hobart and William Smith Colleges)
Hamilton (Colgate University)[2]
Ithaca (Cornell University, Ithaca College)[1]
Morningside Heights, Manhattan (Columbia University, Barnard College, Teachers College, Manhattan School of Music, Jewish Theological Seminary, Union Theological Seminary, Ban
k Street College of Education)
New Paltz (SUNY New Paltz)[2]
Oneonta (SUNY Oneonta, Hartwick College)[2]
Oswego (SUNY Oswego)[2]
Plattsburgh (SUNY Plattsburgh)[2]
Potsdam (SUNY Potsdam, Clarkson University)[2]
Poughkeepsie (Vassar College, Marist College)[2]
Purchase (Purchase College, Manhattanville College)[2]
Rochester (University of Rochester, Rochester Institute of Technology, Nazareth College, St. John Fisher College, Monroe Community College, Roberts Wesleyan College, SUNY Broc
kport, SUNY Empire State College)[2]
Saratoga Springs (Skidmore College)[2]
Seneca Falls (New York Chiropractic College)
Stony Brook (Stony Brook University)
Syracuse (Syracuse University, SUNY ESF, Upstate Medical University)
Tivoli (Bard College)
Troy (Rensselaer Polytechnic Institute, Russell Sage College, Hudson Valley Community College)
West Point (United States Military Academy)
North Carolina[edit]
Banner Elk (Lees-McRae College)
Boiling Springs (Gardner-Webb University)[2]
Boone (Appalachian State University)[2]
Buies Creek (Campbell University)[2]
Chapel Hill (University of North Carolina at Chapel Hill)[20]
Cullowhee (Western Carolina University)[2]
Davidson (Davidson College)[5]
Durham (Duke University, North Carolina Central University)[5]
Elon (Elon University)[2]
Greensboro (University of North Carolina at Greensboro, Greensboro College, Guilford College, North Carolina A & T State University, Bennett College)
Greenville (East Carolina University)[2]
Hickory (Lenoir-Rhyne University)[2]
Mars Hill (Mars Hill College)[2]
Mount Olive (Mount Olive College)[2]
Pembroke (University of North Carolina at Pembroke)[2]
Wilmington, North Carolina (University of North Carolina at Wilmington)
Wingate (Wingate University)[2]
Winston-Salem (Wake Forest University, University of North Carolina School of the Arts, Salem College, Winston-Salem State University)
North Dakota[edit]
Fargo (North Dakota State University)[18]
Grand Forks (University of North Dakota)[5]
Ohio[edit]
Ada (Ohio Northern University)[2]
Alliance (University of Mount Union)
Ashland (Ashland University)[2]
Athens (Ohio University)[2]
Berea (Baldwin Wallace College)
Bluffton (Bluffton University)[2]
Bowling Green (Bowling Green State University)[2]
Cedarville (Cedarville University)[2]
Columbus (Ohio State University)
Delaware (Ohio Wesleyan University)
Fairborn (Wright State University)
Findlay (University of Findlay)
Gambier (Kenyon College)[2]
Granville (Denison University)[2]
Hiram (Hiram College)[2]
Kent (Kent State University)[2]
Nelsonville (Hocking College)[2]
New Concord (Muskingum College)[2]
Oberlin (Oberlin College)[5]
Oxford (Miami University)[5]
Rio Grande (University of Rio Grande)[2]
Wilberforce (Wilberforce University, Central State University)[2]
Oklahoma[edit]
Ada (East Central University)[2]
Alva (Northwestern Oklahoma State University)[2]
Durant (Southeastern Oklahoma State University)[2]
Edmond (University of Central Oklahoma, Oklahoma Christian University)[2]
Goodwell (Oklahoma Panhandle State University)[2]
Langston (Langston University)[5]
Norman (University of Oklahoma)[1]
Stillwater (Oklahoma State University)[5]
Tahlequah (Northeastern State University)[2]
Tulsa (The University of Tulsa)
Weatherford (Southwestern Oklahoma State University)
Oregon[edit]
Ashland (Southern Oregon University)[2]
Corvallis (Oregon State University)[20]
Eugene (Lane Community College, Northwest Christian University, University of Oregon)[20]
Forest Grove (Pacific University)
Klamath Falls (Klamath Community College, Oregon Institute of Technology)
La Grande (Eastern Oregon University)[2]
Marylhurst (Marylhurst University)
McMinnville (Linfield College)
Monmouth (Western Oregon University)[2]
Newberg (George Fox University)
Pennsylvania[edit]
Altoona (Penn State Altoona)
Annville (Lebanon Valley College)[2]
Bethlehem (Lehigh University, Moravian College)
Bloomsburg (Bloomsburg University of Pennsylvania)[2]
Bradford (University of Pittsburgh at Bradford)
California (California University of Pennsylvania)[2]
Carlisle (Dickinson College)
Cecil B. Moore, Philadelphia, also known as "Templetown" (Temple University)
Clarion (Clarion University of Pennsylvania)[2]
Collegeville (Ursinus College)
Cresson (Mount Aloysius College)[2]
East Stroudsburg (East Stroudsburg University of Pennsylvania)[2]
Edinboro (Edinboro University of Pennsylvania)[2]
Erie (Gannon University, Mercyhurst College, Penn State Erie)
Gettysburg (Gettysburg College)[2]
Greensburg (Seton Hill University, University of Pittsburgh at Greensburg)
Grove City (Grove City College)[2]
Huntingdon (Juniata College)[2]
Indiana (Indiana University of Pennsylvania)[2]
Johnstown (University of Pittsburgh at Johnstown)
Kutztown (Kutztown University of Pennsylvania)[2]
Lancaster (Franklin & Marshall)
Lewisburg (Bucknell University)[5]
Lock Haven (Lock Haven University of Pennsylvania)[2]
Loretto (St. Francis University)[2]
Mansfield (Mansfield University of Pennsylvania)[2]
Meadville (Allegheny College)
Mont Alto (Penn State Mont Alto)
Millersville (Millersville University of Pennsylvania)[2]
New Wilmington (Westminster College)[2]
North East (Mercyhurst North East)
University City, Philadelphia (Drexel University, University of Pennsylvania, University of the Sciences in Philadelphia)
Oakland, Pittsburgh (Carnegie Mellon University, University of Pittsburgh, Carlow University)
Reading (Albright College, Alvernia University, Penn State Berks)
Selinsgrove (Susquehanna University)[2]
Shippensburg (Shippensburg University of Pennsylvania)[2]
Slippery Rock (Slippery Rock University of Pennsylvania)[2]
State College (Pennsylvania State University)[22]
Villanova (Villanova University)
Waynesburg (Waynesburg University)
West Chester (West Chester University of Pennsylvania)
Wilkes-Barre (King's College, Wilkes University)
Williamsport (Lycoming College, Pennsylvania College of Technology)[2]
Rhode Island[edit]
Kingston (University of Rhode Island)[2]
Providence (Brown University, (University of Rhode Island), Rhode Island School of Design, Johnson and Wales University, Providence College, Community College of Rhode Island,
Rhode Island College, and Roger Williams University.)
South Carolina[edit]
Central (Southern Wesleyan University)[2]
Charleston (College of Charleston, The Citadel, MUSC)
Clemson (Clemson University)[2]
Clinton (Presbyterian College)
Columbia (University of South Carolina)[14]
Due West (Erskine College)
Florence (Francis Marion University)
Greenwood (Lander University)
Orangeburg (South Carolina State University, Claflin University)[2]
Rock Hill (Winthrop University)
Spartanburg (Wofford College, Converse College, University of South Carolina Upstate, Spartanburg Methodist College, Edward Via College of Osteopathic Medicine, Spartanburg Co
mmunity College, Virginia College, Sherman College of Chiropractic)
South Dakota[edit]
Brookings (South Dakota State University)[2]
Madison (Dakota State University)
Spearfish (Black Hills State University)
Vermillion (University of South Dakota)[5]
Tennessee[edit]
Chattanooga (University of Tennessee at Chattanooga)
Collegedale (Southern Adventist University)
Cookeville (Tennessee Technological University)[2]
Harrogate (Lincoln Memorial University)[2]
Henderson (Freed-Hardeman University)[2]
Johnson City (East Tennessee State University)
Knoxville (University of Tennessee)
Martin (University of Tennessee at Martin)[2]
McKenzie (Bethel University)[2]
Memphis (Christian Brothers University, LeMoyne-Owen College, Memphis College of Art, Memphis Theological Seminary, Rhodes College, Southern College of Optometry, Southwest Te
nnessee Community College, University of Memphis, University of Tennessee Health Science Center, Visible Music College)
Murfreesboro (Middle Tennessee State University)[2]
Nashville (Vanderbilt University, Belmont University, Tennessee State University, Lipscomb University, Fisk University, Aquinas College, Trevecca Nazarene University)
Sewanee (Sewanee: the University of the South)[2]
Texas[edit]
Abilene (Abilene Christian University, Hardin-Simmons University, McMurry University)
Alpine (Sul Ross State University)[2]
Austin (University of Texas at Austin, St. Edwards University, Huston-Tillotson University)[2]
Beaumont (Lamar University)
Canyon (West Texas A&M University)[2]
College Station (Texas A&M University)[5]
Commerce (Texas A&M University–Commerce)[2]
Dallas (Southern Methodist University)
Denton (University of North Texas, Texas Woman's University)[2]
Fort Worth (Texas Christian University, Texas Wesleyan University)
Georgetown (Southwestern University)
Huntsville (Sam Houston State University)[2]
Houston (University of Houston, Texas Southern University, Rice University, Houston Baptist University)
Keene (Southwestern Adventist University)[2]
Kingsville (Texas A&M University–Kingsville)[2]
Lubbock (Texas Tech University, Lubbock Christian University)
Nacogdoches (Stephen F. Austin State University)[2]
Plainview (Wayland Baptist University)[2]
Prairie View (Prairie View A&M University)[2]
San Marcos (Texas State University)[5]
Stephenville (Tarleton State University)[2]
Waco (Baylor University)
Utah[edit]
Cedar City (Southern Utah University)[2]
Logan (Utah State University)[2]
Provo (Brigham Young University)[5]
Orem (Utah Valley University)
Salt Lake City (University of Utah)
Ephraim (Snow College)
Vermont[edit]
Burlington (University of Vermont, Champlain College and Saint Michael's College)[2]
Castleton (Castleton State College)[2]
Johnson (Johnson State College)[2]
Lyndonville (Lyndon State College)[2]
Middlebury (Middlebury College)[2]
Northfield (Norwich University)[2]
Virginia[edit]
Blacksburg (Virginia Polytechnic Institute and State University)[5]
Bridgewater (Bridgewater College)[2]
Charlottesville (University of Virginia)[23]
Farmville (Longwood University, Hampden-Sydney College)[2]
Fredericksburg (University of Mary Washington)[2]
Harrisonburg (James Madison University, Eastern Mennonite University)[2]
Lexington (Washington and Lee University, Virginia Military Institute)[2]
Lynchburg (Lynchburg College, Randolph College, Liberty University, Central Virginia Community College)
Radford (Radford University)[2]
Williamsburg (The College of William & Mary)[2]
Wise (University of Virginia's College at Wise)[2]
Chesapeake (Averett University, DeVry University, Troy University, Tidewater Community College, Strayer University, Everest University, Sentera College of Health Sciences, St
Leo University)[2]
Washington[edit]
Bellingham (Western Washington University)
Cheney (Eastern Washington University)[2]
Ellensburg (Central Washington University)[5]
Pullman (Washington State University)[5]
University District, Seattle (University of Washington)[5]
West Virginia[edit]
Athens (Concord University)[2]
Buckhannon (West Virginia Wesleyan College)[2]
Fairmont (Fairmont State University)[2]
Glenville (Glenville State College)[2]
Huntington (Marshall University)[2]
Montgomery (West Virginia University Institute of Technology)[2]
Morgantown (West Virginia University)[2]
Shepherdstown (Shepherd University)[2]
West Liberty (West Liberty University)[2]
Wisconsin[edit]
Appleton (Lawrence University)
Eau Claire (University of Wisconsin–Eau Claire)
Green Bay (University of Wisconsin-Green Bay)
La Crosse (University of Wisconsin–La Crosse, Western Technical College, Viterbo University)[2]
Madison (University of Wisconsin–Madison)[2]
Menomonie (University of Wisconsin–Stout)[2]
Milwaukee (Marquette University, University of Wisconsin–Milwaukee)
Oshkosh (University of Wisconsin–Oshkosh)
Platteville (University of Wisconsin–Platteville)[2]
River Falls (University of Wisconsin–River Falls)[2]
Stevens Point (University of Wisconsin–Stevens Point)[2]
Waukesha (Carroll University)
Whitewater (University of Wisconsin–Whitewater)[2]
Wyoming[edit]
Laramie (University of Wyoming)[5]

2 Créer un pandas data frame à partir d'un fichier txt


On veut créer un DataFrame nommé towns_df à partir du fichier 'university_towns.txt'

Nous voyons que nous avons des noms d'états périodiques suivis des villes universitaires de cet état: StateA TownA1 TownA2 StateB TownB1 TownB2 .... Si nous regardons la façon dont les noms d'états sont écrits
dans le fichier, nous verrons qu'ils ont tous la sous-chaîne «[edit]» en eux.

Nous pouvons tirer parti de ce modèle en créant une liste de tuples (état, ville) et en enveloppant cette liste dans un DataFrame:

In [14]: university_towns = []
with open('Datasets/university_towns.txt') as file:
for line in file:
if '[edit]' in line:
# se rappeler de ce `state` jusqu'à ce qu'on trouve le suivant
pass
else:
# Sinon, nous avons une ville; garder `state`
pass

university_towns[:5]

Out[14]: [('Alabama[edit]\n', 'Auburn (Auburn University)[1]\n'),


('Alabama[edit]\n', 'Florence (University of North Alabama)\n'),
('Alabama[edit]\n', 'Jacksonville (Jacksonville State University)[2]\n'),
('Alabama[edit]\n', 'Livingston (University of West Alabama)[2]\n'),
('Alabama[edit]\n', 'Montevallo (University of Montevallo)[2]\n')]

In [15]: towns_df = pd.DataFrame(university_towns,


columns=['State', 'RegionName'])
towns_df.head()

Out[15]: State RegionName

0 Alabama[edit]\n Auburn (Auburn University)[1]\n

1 Alabama[edit]\n Florence (University of North Alabama)\n

2 Alabama[edit]\n Jacksonville (Jacksonville State University)[2]\n

3 Alabama[edit]\n Livingston (University of West Alabama)[2]\n

4 Alabama[edit]\n Montevallo (University of Montevallo)[2]\n

3 Compléter la fonction clean_up pour nettoyer les colonnes de towns_df


In [16]: ## complétrer la fonction suivante ( à la place de pass)
def clean_up(item):
if '(' in item:
pass
return item[:item.find('(') - 1]

if '[' in item:
return item[:item.find('[')]

towns_df = towns_df.applymap(clean_up)
towns_df

Out[16]: State RegionName

0 Alabama Auburn

1 Alabama Florence

2 Alabama Jacksonville

3 Alabama Livingston

4 Alabama Montevallo

... ... ...

512 Wisconsin River Falls

513 Wisconsin Stevens Point

514 Wisconsin Waukesha

515 Wisconsin Whitewater

516 Wyoming Laramie

517 rows × 2 columns

PART III ( OPTIONNELLE)


1 Renaming columns and skipping rows
1.1 Convertir le fichier olympics.csv en dataframe pandas olympics_df

In [17]: # exporter 'olympics.csv' sous forme de data frame nommé olympics_df

# Afficher les 5 premières lignes

Out[17]: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

0 NaN ? Summer 01 ! 02 ! 03 ! Total ? Winter 01 ! 02 ! 03 ! Total ? Games 01 ! 02 ! 03 ! Combined total

1 Afghanistan (AFG) 13 0 0 2 2 0 0 0 0 0 13 0 0 2 2

2 Algeria (ALG) 12 5 2 8 15 3 0 0 0 0 15 5 2 8 15

3 Argentina (ARG) 23 18 24 28 70 18 0 0 0 0 41 18 24 28 70

4 Armenia (ARM) 5 1 2 9 12 6 0 0 0 0 11 1 2 9 12

1.2 Supprimer la première ligne


In [21]: # exporter 'olympics.csv' sous forme de data frame nommé olympics_df en supprimant la première ligne

olympics_df = pd.read_csv('./Datasets/olympics.csv', skiprows = 1, header = 0)

# Afficher les 5 premières lignes


olympics_df.head()

Out[21]: Unnamed: 0 ? Summer 01 ! 02 ! 03 ! Total ? Winter 01 !.1 02 !.1 03 !.1 Total.1 ? Games 01 !.2 02 !.2 03 !.2 Combined total

0 Afghanistan (AFG) 13 0 0 2 2 0 0 0 0 0 13 0 0 2 2

1 Algeria (ALG) 12 5 2 8 15 3 0 0 0 0 15 5 2 8 15

2 Argentina (ARG) 23 18 24 28 70 18 0 0 0 0 41 18 24 28 70

3 Armenia (ARM) 5 1 2 9 12 6 0 0 0 0 11 1 2 9 12

4 Australasia (ANZ) [ANZ] 2 3 4 5 12 0 0 0 0 0 2 3 4 5 12

1.3 Changer le nom des colonnes en utilisant le dictionnaire suivant :


new_names = {'Unnamed: 0': 'Country',
'? Summer': 'Summer Olympics',
'01 !': 'Gold',
'02 !': 'Silver',
'03 !': 'Bronze',
'? Winter': 'Winter Olympics',
'01 !.1': 'Gold.1',
'02 !.1': 'Silver.1',
'03 !.1': 'Bronze.1',
'? Games': '# Games',
'01 !.2': 'Gold.2',
'02 !.2': 'Silver.2',
'03 !.2': 'Bronze.2'}

In [10]: # Changer le nom des colonnes en utilisant le dictionnaire new_names

# Afficher les 5 premières lignes

In [23]: olympics_df.head()

Out[23]: Country Summer Olympics Gold Silver Bronze Total Winter Olympics Gold.1 Silver.1 Bronze.1 Total.1 # Games Gold.2 Silver.2 Bronze.2 Combined total

0 Afghanistan (AFG) 13 0 0 2 2 0 0 0 0 0 13 0 0 2 2

1 Algeria (ALG) 12 5 2 8 15 3 0 0 0 0 15 5 2 8 15

2 Argentina (ARG) 23 18 24 28 70 18 0 0 0 0 41 18 24 28 70

3 Armenia (ARM) 5 1 2 9 12 6 0 0 0 0 11 1 2 9 12

4 Australasia (ANZ) [ANZ] 2 3 4 5 12 0 0 0 0 0 2 3 4 5 12

In [ ]:

You might also like