datacamp data types for data science - amazon s3 · datacamp data types for data science adding and...
TRANSCRIPT
DataCamp DataTypesforDataScience
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
DataCamp DataTypesforDataScience
Safelyfindingbykey
GettingavaluefromadictionaryisdoneusingthekeyasanindexIfyouaskforakeythatdoesnotexistthatwillstopyourprogramfromrunninginaKeyError
In[4]:art_galleries['Louvre']---------------------------------------------------------------------------KeyErrorTraceback(mostrecentcalllast)<ipython-input-1-4f51c265f287>in<module>()---->1art_galleries['Louvre']
KeyError:'Louvre'
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'
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'
DataCamp DataTypesforDataScience
Let'spractice!
DATATYPESFORDATASCIENCE
DataCamp DataTypesforDataScience
Alteringdictionaries
DATATYPESFORDATASCIENCE
JasonMyersInstructor
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'}
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'}
DataCamp DataTypesforDataScience
Let'spractice!
DATATYPESFORDATASCIENCE
DataCamp DataTypesforDataScience
Pythonicallyusingdictionaries
DATATYPESFORDATASCIENCE
JasonMyersInstructor
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'
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'}
DataCamp DataTypesforDataScience
Let'spractice!
DATATYPESFORDATASCIENCE
DataCamp DataTypesforDataScience
WorkingwithCSVfiles
DATATYPESFORDATASCIENCE
JasonMyersInstructor
DataCamp DataTypesforDataScience
CSVFilesNAME,TEL,ADDRESS1,ADDRESS2,CITY,ZIPO'reillyWilliam&CoLtd,(212)396-1822,52E76thSt,,NewYork,10021
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()
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()
DataCamp DataTypesforDataScience
Let'spractice!
DATATYPESFORDATASCIENCE