1



KENDRIYA VIDYALAYA…………Assignment-1(Python Pandas-Series Object and Data Frame)1. Answer the following (a) If a Python list is having 7 integers and a numpy array is also having 7 integers, then how are these two data structures similar or different from one another?Answer:-Similarities: They both have elements indexed in the memory and can be indexes to access them. They both have all the elements of the same data type.Differences: Size of the list can be changed while that of the Numpy array can not.The numpy array is memory efficient. i.e takes less space than the python list even though both have the same 7 elements. Numpy is faster . Also numpy supports vetcorized operations while the list does not .ie. operation on the Numpy are applied on all the individual elements.(b) How is a series object different from and similar to ndarrays ? Support your answer with examplesAnswer: Similarity is that both work as an array. Differences are elements in Numpy arrays are accessed by their integer position starting with zero for the first element. A pandas Series Object is more flexible as you can use define your own labeled index to index and access elements of an array. ... Second, aligning data from different Series and matching labels with Series objects is more efficient than using ndarrays, for example dealing with missing values. If there are no matching labels during alignment, pandas returns NaN(not any number) so that the operation does not fail. E,g:Import numpy as npImport pandas as pda=np.array([1,2,3,4])print(a[0]) # index is from 0 to 3b=pd.Series([1,2,3,4], index=[10,20,30,40])print(a[20]) # index is custom and can be anythingc=pd.Series([1,np.nan,3[) # NaN can ne used(c.) Consider the following python code and write the output : import pandas as pdK=pd.Series([2,4,6,8,10])s = pd.Series(K)print (s)Answer: NOTE:-In Series object, index is displayed along with 1dNumpy array where as the same 1dNumpy array display without index labelsimport numpy as nps=np.array([2,4,6,8,10])print (s)OUTPUT:[ 2 4 6 8 10](d) Consider the following Series object, Comp_amtMouse 150Keyboard 300Pen drive 800CD 20i. Write the command to display the name of the items having amount <500.ii. Write the command to name the series as IT_equipments.Answer: print(Comp_amt[Comp_amt<500])Comp_amt.name=’IT_equipments’2. (a) Given are two objects, namely LIST1 and a series object namely SERIES1, both are having similar values i.e 2,4,6,8. Find out the output produced by following statements:i. print(LIST1*2)ii. print(SERIES1)Answer:-[2, 4, 6, 8, 2, 4, 6, 8] ii) (b) Write Python code to create a series object TEMP storing temperatures of 7 days of week. Its indexes should be‘Sunday,’ .’Monday’,……………………’Saturday’Answer:-import pandas as pddays=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']TEMP=pd.Series([98,99,99.5,100,100.4,95.2,104],index=days)print(TEMP)(c.) Consider a given Series , S1:1797685372745001083310535305Index00IndexAvg_SalaryUP5000MP6000Gujarat8000Delhi5500Write a program in Python Pandas to create the series.Answer:-import pandas as pdS1=pd.Series([5000,6000,8000,5500],index=['UP','MP’,'Gujarat','Delhi'])3. (a) Write a python statement to create a dataframe for the following: Answer:import pandas as pd data=[['Rajiv',20,'Clerk'],['Sameer',35,'Manager'],['Kapil',45,'Accountant']]df1=pd.DataFrame(data,columns=['Name','Age','Designation'])print(df1)(b) How would you add a new column namely ‘VAL’ to a data frame ,DF that has 10 rows in it and has columns as ‘ITEM’,’QTY’,’PRICE’ ? You can choose to put any values of your choice. Answer:- import pandas as pddata={'ITEM':[1,2,3,4,5,6,7,8,9,10], 'QTY':[5,7,2,8,10,9,12,20,11,22], 'PRICE':[100,200,50,80,70,40,90,80,150,300]}Df1=pd.DataFrame(data)Df1['VAL']=[ i for i in range(10,20)]print(Df1)(c.) Consider the following DataFrame, classframeRollnoNameClassSectionCGPAStreamSt11NareshIXA8.7ScienceSt22LakshayXIIB8.9ArtsSt33TraptiXC9.2ScienceSt44PrakharXIB9.4CommerceWrite commands to :i. Add a new column ‘Activity’ to the Dataframeii. Add a new row with values ( 5 , Shailly, XII, D , 9.8, Arts)Answer:-classframe[‘Activity’]=[‘Swimming’, ’Dancing’, ’Cricket’, ‘Singing’]classframe.loc[‘St5’]= [5,’Shailly’, ‘XI’, ‘B’, 9.4, ‘Commerce’](d) Write a program in Python Pandas to create the following DataFrame batsman from a Dictionary:B_noNameScore1Score21Sachin90802Dhoni65453Kapil70904Rahul8076Perform the following operations on the DataFrame :1) Add both the scores of a batsman and assign to column “Total”2) Display the highest score in both Score1 and Score2 of the DataFrame. 3) Display the DataFrameAnswer:-import pandas as pd data={ 'B_No':[1,2,3,4], 'Name':["Sachin","Dhoni","Kapil","Rahul"],'Score1':[90,65,70,80], 'Score2':[80,45,90,76] } df=pd.DataFrame(data) print(df) 1) df['Total'] = df['Score1']+ df['Score2'] Alternative Answer Scheme df['Total'] = sum(df['Score1'], df['Score2']) print(df) 2) print("Maximum scores are : " , max(df['Score1']), max(df['Score2']))(e.) Give the output for the following code.import pandas as pddata = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]#With two column indices, values same as dictionary keydf1 = pd.DataFrame(data, index=['first', 'second'],columns=['a', 'b'])#With two column indices with one index with other namedf2 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1']) print(df1)print(df2)Answer:-(d) Give the output for the above statement.import pandas as pd data = [1,2,3,4,5]df = pd.DataFrame(data) print(df)Answer:-(e) Give the output for the above statement.a=pd.DataFrame([1,1, 1, np.nan], index=['a', 'b', 'c', 'd'],columns=['one'])Answer:-(g) What is the output of the following code: df1= pd. DataFrame ([1, 2 , 3])df2= pd. DataFrame ([[1, 2, 3]])print("df1")print(df1)print("df2")print(df2)Answer:-3528060338455ColorCountPriceAppleRed3120AppleGreen9110PearRed25125PearGreen26150LimeGreen99 7000ColorCountPriceAppleRed3120AppleGreen9110PearRed25125PearGreen26150LimeGreen99 704. Given a data frame namely data as shown in adjacent figure (fruit names are row labels). Write code statement to : List fruits with count more than 25.List 2nd, 3rd and 4th rows. Find all rows with the label”Apple”.Extract all columnsAnswer:-import pandas as pddata={ 'Color': ['Red','Green','Red','Green','Green'], 'Count':[3,9,25,26,99], 'Price':[120,110,125,150,70] }df1=pd.DataFrame(data)df1.index=['Apple','Apple','Pear','Pear','Lime']i) df1[df1[‘count’]>25ii) df1.iloc[2:5]iii) df1.loc[‘Apple’,:]5. Find the output of the following code:import pandas as pddata = [{'Rollno': 1, 'Name': 'Shyam', 'Marks':60 }, {'Rollno': 2, 'Name': 'Kamal', 'Marks':70 }, {'Rollno': 5, 'Name': 'Sheela', 'Marks':30 }]df1 = pd.DataFrame(data, columns=['Rollno', 'Name'])df2 = pd.DataFrame(data, index=['Rollno', 'Name', 'Marks'],columns=['Rollno', 'Marks'])print(df1)print(df2)Answer:-6. Write a Pandas program to create and display a Data Frame from a specified dictionary data which has the index labels.exam_data = {'name': ['Dima', 'James', 'Emily', 'Laura', 'Kevin'], 'score': [9, 16.5, np.nan, 14.5, np.nan], 'attempts': [1, 3, 2, 3, 1], 'qualify': ['yes', 'no', 'yes', 'no', 'no']} labels = ['a', 'b', 'c', 'd', 'e'] Answer:- 7. Write a Pandas program to change the score in row 'd' to 11.5.exam = {'name': ['Anastasia', 'Dima', 'Emily', 'Laura', 'Kevin'],'score': [np.nan, 9, 20, 14.5, np.nan, 19], 'attempts': [1, 3, 2, 3, 2], 'qualify':['no','yes','yes','no','no']}labels = ['a', 'b', 'd', ‘i', 'j'] Answer: 7.(i) Write a Python code to create the following Dataframe Empdf from a Dictionary: EName Salary0 Kush 100001 Ruchika120002 Divya 200003John25000(ii) Write python code to display the Ename and Salary having Salary more than 20000.(iii) Write python code to add a column ‘Commission’ with values as 5% of the Salary.(iv) Write python code to display the dataframe.Answer:-8. Find the index position where the minimum and maximum value exist in Pandas Data FrameAnswer:9. 10. How to writing Data Frame to CSV files in pandas? 11. What is the output of the following Python code?12. A data frame STU stores details like ‘NAME’, ‘CLASS’,’SUBJECT_ID’ for 10 students and another data frame ‘MARKS’ stores details like ‘SUBJECT_ID’ and ‘AVG_MARKS’ Write code so that two data frames combine data on the basis of common ‘SUBJECT_ID.Answer:(e) The head() and tail() extract rows or columns from a data frame ExplainAnswer:(f) Why does Python change the data type of a column as soon as it stores an empty value (NaN) even though it has all other values stored as integer?Answer:-14. Consider the following dataframe df as shown below:Name engip geo totalT1kushagra 52 98 85 235T2 naresh 48 85 88 221T3 prakhar 69 94 78 241T4 trapti 70 81 91 242 (i) Write the code to create above dataframe in Python (Pandas code).(ii) What will be the output produced by following statements?>>> print(df.at['T3','total'], df.at['T1','ip']) 235 94 241 98 241 94 235 98(iii) What will be the output produced by following statements?>>> print(df.loc['T2' : 'T3' , 'ip':'geo' ]) (i) ip geoT2 85 88T3 94 78(ii) ip T2 85 (iii) ip geoT2 85 88(iv) ip T2 85 T3 94 (iv) What will be the output produced by following statements?>>> print(df.iat[2,1], df.iat[1,2]) prakhar 69 T2 naresh kushagra 52 69 85(v) What will be the output produced by following statements?>>> print(df.iloc[ : : 2, 0 : : 4 ])(i) name totalT1 kushagra 235T2 naresh 221(ii) name totalT2 naresh 221T4 trapti 242(iii) name totalT1 kushagra 235T3 prakhar 241(iii) name totalT3 prakhar 241T4 trapti 242 ................
................

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

Google Online Preview   Download