datacamp data types for data science - amazon s3 · datacamp data types for data science adding and...

19
DataCamp Data Types for Data Science

Upload: others

Post on 02-Aug-2020

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

Page 2: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

CreatingandloopingthroughdictionariesHolddatainkey/valuepairsNestable(useadictionaryasthevalueofakeywithinadictionary)IterableCreatedbydict()or{}

In[1]:art_galleries={}

In[2]:forname,zip_codeingalleries:...:art_galleries[name]=zip_code

In[3]:fornameinart_galleries:...:print(name)ZwirnerDavidGalleryZwirner&WirthZitoStudioGalleryZetterquistGalleriesZarreAndreGallery

Page 3: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

Safelyfindingbykey

GettingavaluefromadictionaryisdoneusingthekeyasanindexIfyouaskforakeythatdoesnotexistthatwillstopyourprogramfromrunninginaKeyError

In[4]:art_galleries['Louvre']---------------------------------------------------------------------------KeyErrorTraceback(mostrecentcalllast)<ipython-input-1-4f51c265f287>in<module>()---->1art_galleries['Louvre']

KeyError:'Louvre'

Page 4: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

Safelyfindingbykey(cont.).get()methodallowsyoutosafelyaccessakeywithouterroror

exceptionhandlingIfakeyisnotinthedictionary,.get()returnsNonebydefaultoryou

cansupplyavaluetoreturnIn[5]:art_galleries.get('Louvre','NotFound')Out[5]:'NotFound'

In[6]:art_galleries.get('ZarreAndreGallery')Out[6]:'10011'

Page 5: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

Workingwithnesteddata

The.keys()methodshowsthekeysforagivendictionary

CommonwaytodealwithrepeatingdatastructuresCanbeaccessedusingmultipleindicesorthe.get()method

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'sArtGallery":'(212)531-1577','TripleCandie':'(212)865-0783','AfricartMotherlandInc':'(212)368-6802','InnerCityArtGalleryInc':'(212)368-4941'}

In[3]:art_galleries['10027']['InnerCityArtGalleryInc']Out[3]:'(212)368-4941'

Page 6: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

Let'spractice!

DATATYPESFORDATASCIENCE

Page 7: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

Alteringdictionaries

DATATYPESFORDATASCIENCE

JasonMyersInstructor

Page 8: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

AddingandextendingdictionariesAssignmenttoaddanewkey/valuetoadictionary.update()methodtoupdateadictionaryfromanotherdictionary,

tuplesorkeywordsIn[1]:print(galleries_10007){'NyabinghiAfricianGiftShop':'(212)566-3336'}

In[2]:art_galleries['10007']=galleries_10007

In[3]:galleries_11234=[('AJARTSLTD','(718)763-5473'),...:('DougMeyerFineArt','(718)375-8006'),...:('PortraitGallery','(718)377-8762')]

In[4]:art_galleries['11234'].update(galleries_11234)

In[5]:print(art_galleries['11234']){'PortraitGallery':'(718)377-8762','AJARTSLTD':'(718)763-5473','DougMeyerFineArt':'(718)375-8006'}

Page 9: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

Poppinganddeletingfromdictionariesdelinstructiondeletesakey/value

.pop()methodsafelyremovesakey/valuefromadictionary.In[1]:delart_galleries['11234']

In[2]:galleries_10310=art_galleries.pop('10310')

In[3]:print(galleries_10310){'NewDorpVillageAntiquesLtd':'(718)815-2526'}

Page 10: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

Let'spractice!

DATATYPESFORDATASCIENCE

Page 11: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

Pythonicallyusingdictionaries

DATATYPESFORDATASCIENCE

JasonMyersInstructor

Page 12: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

Workingwithdictionariesmorepythonically.items()methodreturnsanobjectwecaniterateover

In[1]:forgallery,phone_numinart_galleries.items():...:print(gallery)...:print(phone_num)'MiakeyArtGallery''(718)686-0788''MorningStarGalleryLtd''(212)334-9330'}'NewYorkArtExpoInc''(212)363-8280'

Page 13: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

Checkingdictionariesfordata.get()doesalotofworktocheckforakey

inoperatorismuchmoreefficientandclearerIn[1]:'11234'inart_galleriesOut[1]:False

In[2]:if'10010'inart_galleries:...:print('Ifound:%s'%art_galleries['10010'])...:else:...:print('Nogalleriesfound.')Ifound:{'NyabinghiAfricianGiftShop':'(212)566-3336'}

Page 14: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

Let'spractice!

DATATYPESFORDATASCIENCE

Page 15: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

WorkingwithCSVfiles

DATATYPESFORDATASCIENCE

JasonMyersInstructor

Page 16: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

CSVFilesNAME,TEL,ADDRESS1,ADDRESS2,CITY,ZIPO'reillyWilliam&CoLtd,(212)396-1822,52E76thSt,,NewYork,10021

Page 17: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

ReadingfromafileusingCSVreaderPythoncsvmodule

open()functionprovidesavariablethatrepresentsafile,takesapath

andamodecsv.reader()readsafileobjectandreturnsthelinesfromthefileas

tuples.close()methodclosesfileobjects

In[1]:importcsv

In[2]:csvfile=open('ART_GALLERY.csv','r')

In[3]:forrowincsv.reader(csvfile):...:print(row)['NAME','the_geom','TEL','URL','ADDRESS1','ADDRESS2','CITY','ZIP']["O'reillyWilliam&CoLtd",'POINT(-73.9627307456199640.773800871637576)','(212)396-1822','52E76thSt','','NewYork','10021']

In[4]:csvfile.close()

Page 18: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

CreatingadictionaryfromafileOftenwewanttogofromCSVfiletodictionaryDictReaderdoesjustthatIfdatadoesn'thaveaheaderrow,youcanpassinthecolumnnames

In[1]:importcsv

In[2]:csvfile=open('ART_GALLERY.csv','r')

In[3]:forrowincsv.DictReader(csvfile):...:print(row)OrderedDict([('NAME','OdyssiaGallery'),('the_geom','POINT(-73.9626981363555440.7618747512849)'),('TEL','(212)486-7338'),('URL','http://www.livevillage.com/newyork/art/odyssia-gallery.html'),('ADDRESS1','305E61stSt'),('ADDRESS2',''),('CITY','NewYork'),('ZIP','10021')])

In[4]:csvfile.close()

Page 19: DataCamp Data Types for Data Science - Amazon S3 · DataCamp Data Types for Data Science Adding and extending dictionaries Assignment to add a new key/value to a dictionary.update()

DataCamp DataTypesforDataScience

Let'spractice!

DATATYPESFORDATASCIENCE