Notesatfingerbyvrinda.files.wordpress.com



CHAPTER 1- SERIES CREATIONimport pandas as pdimport numpy as nps1=pd.Series()print(s1)s2=pd.Series(dtype=int)print(s2)l=['a','b','c','d']d=np.array(l)s3=pd.Series(d)print(s3)l=['a','b','c','d']d=np.array(l)s4=pd.Series(d,index=[101,102,103,104])print(s4)dict={1:'a',2:'b',3:'c',4:'d'}s5=pd.Series(dict)print(s5)s6=pd.Series(5,index=[1,2,3,4],dtype=int,name='hai')print(s6)ACCESSINGimport pandas as pds = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])print(s[::])#prints allprint(s[::2])#prints all but alternate step=2print(s[::-1])#prints in reverse orderprint(s[1:3:])#prints from index 1 to 2print(s[3:])#prints from index 3 to endprint(s[:3])#prints from 0 to 2print(s[-3:])#prints last 3print(s[-2:])#prints last 2print(s['a'])#prints with label aprint(s[['a','b','c','d','e']])#prints all print(s[['a','b','c']])#prints with label a,b and cSLICE() IN SERIESThe str.slice() function is used to slice substrings from each element in the Series or Index. Series_name.str.slice(start=None, stop=None, step=None)Parameters:NameDescriptionType/Default ValueRequired / OptionalstartStart position for slice operation.intOptionalstopStop position for slice operation.intOptionalstep??Step size for slice operation.intOptionalimport numpy as npimport pandas as pds = pd.Series(["tiger", "cat", "elephant"])print(s)Output:0 tiger1 cat2 elephantdtype: objects.str.slice(start=1)Output:0 iger1 at2 lephantdtype: objects.str.slice(stop=2)Output:0 ti1 ca2 eldtype: object SLICE PROGRAMimport pandas as pds=pd.Series(["1245678","Tiger","!@#$","INFORMATICS"])print(s)print("APPLYING SLICING")print(s.str.slice(1,3)) #start=1 stop=3 so takes upto 2print(s.str.slice(-1))# start=-1 takes only last characterprint(s.str.slice(-1,))MATHEMATICAL OPERATIONS ON SERIESimport pandas as pds1=pd.Series([1,2,3,4])'''print(s1.sum())print(s1.mean())print(s1.min())print(s1.max())print(s1.sum()/s1.count())'''population={'India':300,'America':150,'Briton':100}s2=pd.Series(population)area={'America':10000,'India':1000,'Briton':1000}s3=pd.Series(area)print("Population/Area\n",s2/s3)HEAD AND TAILimport pandas as pdpopulation={'India':300,'America':150,'Briton':100}s2=pd.Series(population)area={'America':10000,'India':1000,'Briton':1000}s3=pd.Series(area)print("++++++++++++++")print("s2")print(s2)print("++++++++++++++")print("Head(1)")print(s2.head(1))print("++++++++++++++")print("s3")print(s3)print("++++++++++++++")print("Tail(2)")print(s3.tail(2)) ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download