DataCamp Data Types for Data Science DATA TYPES FOR DATA SCIENCE Using dictionaries Jason Myers Instructor
DataCamp Data Types for Data Science Creating and looping through dictionaries Hold data in key/value pairs Nestable (use a dictionary as the value of a key within a dictionary) Iterable Created by dict() or {} In [1]: art_galleries = {} In [2]: for name, zip_code in galleries: ...: art_galleries[name] = zip_code In [3]: for name in art_galleries: ...: print(name) Zwirner David Gallery Zwirner & Wirth Zito Studio Gallery Zetterquist Galleries Zarre Andre Gallery
DataCamp Data Types for Data Science Safely finding by key In [4]: art_galleries['Louvre'] --------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-1-4f51c265f287> in <module>() ----> 1 art_galleries['Louvre'] KeyError: 'Louvre' Getting a value from a dictionary is done using the key as an index If you ask for a key that does not exist that will stop your program from running in a KeyError
DataCamp Data Types for Data Science Safely finding by key (cont.) .get() method allows you to safely access a key without error or exception handling If a key is not in the dictionary, .get() returns None by default or you can supply a value to return In [5]: art_galleries.get('Louvre', 'Not Found') Out[5]: 'Not Found' In [6]: art_galleries.get('Zarre Andre Gallery') Out[6]: '10011'
DataCamp Data Types for Data Science Working with nested data In [1]: art_galleries.keys() Out[1]: dict_keys(['10021', '10013', '10001', '10009', '10011', '10022', ...: '10027', '10019', '11106', '10128']) In [2]: print(art_galleries['10027']) {"Paige's Art Gallery": '(212) 531-1577', 'Triple Candie': '(212) 865-0783', 'Africart Motherland Inc': '(212) 368-6802', 'Inner City Art Gallery Inc': '(212) 368-4941'} In [3]: art_galleries['10027']['Inner City Art Gallery Inc'] Out[3]: '(212) 368-4941' The .keys() method shows the keys for a given dictionary Common way to deal with repeating data structures Can be accessed using multiple indices or the .get() method
DataCamp Data Types for Data Science DATA TYPES FOR DATA SCIENCE Let's practice!
DataCamp Data Types for Data Science DATA TYPES FOR DATA SCIENCE Altering dictionaries Jason Myers Instructor
DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary .update() method to update a dictionary from another dictionary, tuples or keywords In [1]: print(galleries_10007) {'Nyabinghi Africian Gift Shop': '(212) 566-3336'} In [2]: art_galleries['10007'] = galleries_10007 In [3]: galleries_11234 = [('A J ARTS LTD', '(718) 763-5473'), ...: ('Doug Meyer Fine Art', '(718) 375-8006'), ...: ('Portrait Gallery', '(718) 377-8762')] In [4]: art_galleries['11234'].update(galleries_11234) In [5]: print(art_galleries['11234']) {'Portrait Gallery': '(718) 377-8762', 'A J ARTS LTD': '(718) 763-5473', 'Doug Meyer Fine Art': '(718) 375-8006'}
DataCamp Data Types for Data Science Popping and deleting from dictionaries del instruction deletes a key/value .pop() method safely removes a key/value from a dictionary. In [1]: del art_galleries['11234'] In [2]: galleries_10310 = art_galleries.pop('10310') In [3]: print(galleries_10310) {'New Dorp Village Antiques Ltd': '(718) 815-2526'}
DataCamp Data Types for Data Science DATA TYPES FOR DATA SCIENCE Let's practice!
DataCamp Data Types for Data Science DATA TYPES FOR DATA SCIENCE Pythonically using dictionaries Jason Myers Instructor
DataCamp Data Types for Data Science Working with dictionaries more pythonically .items() method returns an object we can iterate over In [1]: for gallery, phone_num in art_galleries.items(): ...: print(gallery) ...: print(phone_num) 'Miakey Art Gallery' '(718) 686-0788' 'Morning Star Gallery Ltd' '(212) 334-9330'} 'New York Art Expo Inc' '(212) 363-8280'
DataCamp Data Types for Data Science Checking dictionaries for data .get() does a lot of work to check for a key in operator is much more efficient and clearer In [1]: '11234' in art_galleries Out[1]: False In [2]: if '10010' in art_galleries: ...: print('I found: %s' % art_galleries['10010']) ...: else: ...: print('No galleries found.') I found: {'Nyabinghi Africian Gift Shop': '(212) 566-3336'}
DataCamp Data Types for Data Science DATA TYPES FOR DATA SCIENCE Let's practice!
DataCamp Data Types for Data Science DATA TYPES FOR DATA SCIENCE Working with CSV files Jason Myers Instructor
DataCamp Data Types for Data Science CSV Files NAME,TEL,ADDRESS1,ADDRESS2,CITY,ZIP O'reilly William & Co Ltd,(212) 396-1822,52 E 76th St,,New York,10021
DataCamp Data Types for Data Science Reading from a file using CSV reader Python csv module open() function provides a variable that represents a file, takes a path and a mode csv.reader() reads a file object and returns the lines from the file as tuples .close() method closes file objects In [1]: import csv In [2]: csvfile = open('ART_GALLERY.csv', 'r') In [3]: for row in csv.reader(csvfile): ...: print(row) ['NAME', 'the_geom', 'TEL', 'URL', 'ADDRESS1', 'ADDRESS2', 'CITY', 'ZIP'] ["O'reilly William & Co Ltd",'POINT (-73.96273074561996 40.773800871637576)', '(212) 396-1822', '52 E 76th St', '', 'New York', '10021'] In [4]: csvfile.close()
DataCamp Data Types for Data Science Creating a dictionary from a file Often we want to go from CSV file to dictionary DictReader does just that If data doesn't have a header row, you can pass in the column names In [1]: import csv In [2]: csvfile = open('ART_GALLERY.csv', 'r') In [3]: for row in csv.DictReader(csvfile): ...: print(row) OrderedDict([('NAME', 'Odyssia Gallery'), ('the_geom', 'POINT (-73.96269813635554 40.7618747512849)'), ('TEL', '(212) 486-7338'), ('URL', 'http://www.livevillage.com/newyork/art/odyssia-gallery.html'), ('ADDRESS1', '305 E 61st St'), ('ADDRESS2', ''), ('CITY', 'New York'), ('ZIP', '10021')]) In [4]: csvfile.close()
DataCamp Data Types for Data Science DATA TYPES FOR DATA SCIENCE Let's practice!
Recommend
More recommend