Ip Xii

You might also like

Download as pdf
Download as pdf
You are on page 1of 6
EXAMPLE Write code to create a Series abject using the Python sequence [4, 6, 8, 101. Assume that Pandas is imported as alias name pd. SOLUTION import pandas as pd si=pd.Series([4, 6, 8, 10]) Output Series object 1: o 4 print("Series object 1:") 1 6 print(s1) 2 8 3 io dtype : intea EXAMPLE Write code to create a Series object using the Python sequence (11, 21, 31, 41). Assume that Pandas is imported as alias name pd. SOLUTION Output import pandas as pd Series object 2: s2=pd.Series((11, 21, 31, 41)) ° print (“Series object 2: : 2 print(s2) 2 = dtype: int64 ‘Scanned with CamSeanner INFORMATICS react 6 PACTS EXAMPLE BE Witenprogran to cto a Ser object using individual characters‘ 7, and Asm ge Pandas i imported as alias name pl Oeipnt SOLUTION Amport pandas as pd Series oie: S3=pd.series({'o", *h', ‘o"D) Hy = print ("Series object:") 4 print(s3) Atype: object EXAMPLE PAP Wet progr to cate Sve eet wing arin = 0 fmiy™ Assume hat Pan Inport as alias noe pd SOLUTION Output ‘mport pandas as pd series abject: print("Serges objects") 0 So fanny = pd Series("So funny") type: abject print(sa) EXAMPLE EE] Wel a program to crate «Sere bec wing tive diferent words", a “aghng “Assume tht ands i imported a lias name pl an SOLUTION series object: sport pandas a pd . : 55 pd.Series({"T", “an, "Laughing"]) 1 7 print("Sertes object: 2 cane print(s5) ddtype: object (i) Specify data as on ndarray ‘The data atibutecan be an ndaray also, carenge(a, 8, 5) Consider following code: ; dat =. arange(3, 13, 3.5) prine(ndan) sert =pd.Series(ndat) io these print(sera) only You can also combine the code tines of types? types floates above code, Carefully read the following : ser pd.sertos( mp.arange(3, 13, 3.5) ) ~ ‘You can create a Series objet fom any ndavray een, ‘example that does the same. ¥y ndartay created from any function. Consider following ‘Scanned with CamSeanner Copter 1: PYTHON PANDAS — 1 BRAMPLE PAL writen program to ere Series jet wing an HITEY Outpt that has 5 elements i the sage 2 1 6 0 24.0 SOLUTION 4 thy import pandas as pd ete 56 pd, Serdes (ap-Linspace(24, 64, 5)) 7 an print(s6) type: Floaée BRAMPLE Ba] Wie a pram to rete a Series object wsing an dara Ua is eentd by tiling ist [3.5], tie Output SOLUTION ae: import pandas as pd i 7 =pd.Series (np.tile((3,5],2)) 23 peint(s?) 35 type: int32 Ui) Specify date as © Python Dictionary : “The sequence that you provide with Series) can be any sequence, including dictionaries. Let us see how you can create a Series object by specifying indexes and values through a dictionary. (see below) Ba eb Tas, HEAT LY in [a8] obgs © palSerdes (a0 vat is TT =e ae ray = 8 eee types ints ‘i gherin Be ena above) Here, one thing is noteworthy that if you are reating a Series object from a dictionary object, then keys of the dictionary become index ofthe Series and the values of dictionary become the data of Series object, Also, the iudexes, which are created from keys may not be in the same ‘order as you have typed them. EXAMPLE EM write progr to crete a Series objet using tionary hat stores Os abr of talents eh section of lass 12 your sl SOLUTION. Output import pandas as pd a9 stu=(°A'39, “B41, °C°s42, "0": a4) sa 58 «pd.Serses(stu) <2 print(s8) feyoes sees ee ‘Scanned with CamSeanner EXAI F MPLE Write a program to create a Series object thal ‘he initial budget allocated (500001- ench the four quarters of the year : Qtr1, Qtr2, Qtr3 and Qtr4. a SOLUTION Ouest import pandas as pd ey aoe : gtr2 5000¢ 59 = pd.Series (50000, index =[‘gtra", ‘@tr2, ‘gtr3', ‘tra’]) Qtr3 sooco print(s9) qtra 50000 dtype: int64 7 15 200 in the Inter University games held every alternate yea. EXAMPLE, Total number of medals to be won for games to be held in the decade 2020-2029. a Series object that stores these medals, Write code to create SOLUTION Output jc 2020 200 import pandas as pd 2022 200 510 = pd.Series(200, index = range(2020,2029,2)) 2024 200 ‘ 2026 200 print(s1@) soe 200 dtype: int64 ‘Scanned with CamSeanner EXAMPLE ‘A Python list namely section stores te section names A’, ‘B’,‘C’,‘D’) of class 12 in your schoo, Another list contri stores the contribution made by these students to a charity fund endorsed by the school. Write code to create a Series object that stores the contribution amount as the values and the section names as the indexes, SOLUTION Output import pandas as pd section=['A', 'B', 'C', 'D'] e ee contri = [6700, 5620, 5000, 5200] en aac 11 = pd.Series(data = contri, index = section) ow print (si1) : ‘Scanned with CamSeanner EXAMPLE, Sequences section and contril store the section names CA‘, ‘B, 'C’, ‘D', "E') and contribsttion made by them respectively (6700, 5600, 5000, 5200, nil) for a charity. Your school has decided to donate as much contribution as made by each section, i.e., the donation will be doubled. Write code to create a Series object that stores the contribution amount as the values and the section names as the indexes with datatype as float32. SOLUTION import pandas as pd This time we have stored the contri import numpy as np ndarray contril 20 that we can deole section=['A', ‘B', ‘C’, ‘D', 'E'] a vectorized operation to add school’s contr contrii = np.array([6700, 5600, 5000, 5208, np.NaN]) 512+ pd.Series(data = contri *2, index = section, dtype = np.Float32) i v3 print (st?) NN This will double each value of the contril ndarcas. Te done to reflect the school’s contribution 10 ci Output a A 13400.0 * NIQTE 8 1120.0 When you store a NaN value in a series ¢ 10000.0 object, Pandas requires the dat2 tye pb 10400.0 Compare date pe of hs Series objec tobe of floating point type. Even if you E NaN sth the pesous objec (611). Iris now ity ait Gee paatee fren from default dana 1p type: Float32 <—— . ee promote it to a floating point type {automatically} becouse NaN is not supported by integer types. ‘Scanned with CamSeanner

You might also like