Notesatfingerbyvrinda.files.wordpress.com



HOME WORK QUESTIONSSERIESWrite a menu driven program to create pandas series from a list and ndarray.import pandas as pdimport numpy as npl=[]print('+++++++++++MENU+++++++++++++')print('1. CREATE SERIES FROM LIST')print('2. CREATE SERIES FROM NDARRAY')c=int(input('Enter your choice'))n1=int(input('Enter number of element'))for i in range(n1): n2=int(input('Enter element:'))l.append(n2)if c==1: s=pd.Series(l) print('FROM LIST',s)else:ar=np.array(l) s=pd.Series('FROM ARRAY',ar) print(s)Write a program to create pandas series from dictionary.import pandas as pdd={}n1=int(input('Enter number of students'))for i in range(n1): n=input('enter name') n2=int(input('Enter mark:'))d.update({n:n2})s=pd.Series(d)print(s)Write a program to count the even numbers in a series.import pandas as pdimport numpy as npl=[]n1=int(input('Enter the number of elements'))c=0for i in range(n1): n2=int(input('Enter element:'))l.append(n2)s=pd.Series(np.array(l))for i in range(n1): if s[i]%2==0: c=c+1print('Number of even elements:',c)print('Number of odd elements:',n1-c)Write a program to create series and find 75th percentile.import pandas as pdl=[31,10,23,41,40,26,28,19]l.sort()s=pd.Series(l)print(s)s=(s.sort_values(ascending=True))position=(.75)*8 (75/100)*TOTAL NUMBER OF ITEMSprint(position)if position.is_integer():perc=(s[position-1]+s[position])/2else: print(s[round(position)])perc=s[round(position)]print('Percentile',perc)DATAFRAMEWrite a program to create a dataframe from list of dictionaries, to store marks of 2 subjects for five students and find the total mark.import pandas as pdl=[]n=[]t=[]fori in range(5):name=input('Enter name:') m1=int(input('Mark of maths:')) m2=int(input('Mark of Science:'))tot=m1+m2 d={'maths':m1,'science':m2}l.append(d)n.append(name)t.append(tot)df= pd.DataFrame(l,index=n) print(df)df[total]=totprint(df)Modify the above program using dictionary of series.import pandas as pdl=[]n=[]math=[]phy=[]tot=[]fori in range(5):name=input('Enter name:') m1=int(input('Mark of maths:')) m2=int(input('Mark of Science:'))math.append(m1)phy.append(m2)n.append(name)tot.append(m1+m2)print('The dataframe created')dict1={'MATHS':pd.Series(math,index=n),'PHY':pd.Series(phy,index=n)}df= pd.DataFrame(dict1)print(df)print('After finding total mark')df['TOTAL']=totprint(df)Replace all missing values in a data frame with a 999.import pandas as pdd = {'one' :pd.Series([1, 2], index=['a', 'b']), 'two' : pd.Series([1, 2,3,4], index=['a', 'b', 'c', 'd']), 'three': pd.Series([1], index=['a'])}df1 = pd.DataFrame(d)print(df1)df2=df1.fillna(999)print('FILL ALL\n',df2)Create a data frame based on ecommerce data (such as quarterly sales where each row contains the item category, item name, quantity in stock, price per item etc.) and generate descriptive statistics (mean, median, mode)import pandas as pdn1=int(input('Enter the number of items'))Id=[]l=[]fori in range(n1): I=input('Enter item id') n=input('Enter name:') c=input('Category:') q=int(input('Qty:')) p=float(input('Price:')) d={'Category':c,'Name':n,'Quantity':q,'Price':p}l.append(d)Id.append(I)df= pd.DataFrame(l,index=Id) print(df)print('MEAN OF PRICE',df['Price'].mean())print('MEDIAN OF PRICE',df['Price'].median())print('MODE OF PRICE',df['Price'].mode())Create a Data Frame for quarterly sales where each row contains the item category, item name, and expenditure. Group the rows by the category and display the maximum expenditure and total expenditure.import pandas as pdn1=int(input('Enter the number of items'))Id=[]l=[]fori in range(n1): I=input('Enter item id') c=input('Category') n=input('Enter name:') e=float(input('Expenditure:')) d={'Category':c,'Name':n,'Expenditure':e}l.append(d)Id.append(I)df= pd.DataFrame(l,index=Id) print(df)print('Group by Category')print(df.groupby('Category'))print('Total Expenditure by category',df.groupby('Category').sum())print('Maximum Expenditure by category',df.groupby('Category').max())Create a data frame for examination result and display row labels, column labels data types of each column and the dimensions.import?pandas?as?pdl=[]n=[]math=[]phy=[]tot=[]num=int(input('Enterthe?Number?of?students'))for?i?in?range(num):????name=input('Enter?name:')????m1=int(input('Mark?of?maths:'))????m2=int(input('Mark?of?Science:'))????math.append(m1)????phy.append(m2)????n.append(name)????tot.append(m1+m2)print('The?dataframe?created')dict1={'MATHS':pd.Series(math,index=n),'PHY':pd.Series(phy,index=n)}df=?pd.DataFrame(dict1)print(df)print('After?finding?total?mark')df['TOTAL']=totprint(df)#to?read?column?and?rowlabelscol_label=df.columns.valuesprint('Column?Names',col_label)row_label=df.index.valuesprint('Row?Names',row_label)print('Dimension?of?dataframe',df.shape)r,c=df.shapeprint('Numberof?rows',r)print('Numberof?columns',c)Create a dataframe for storing name year of joining and salary of 5 employees. Then Display the dataframe,After sorting(based on any column).After filtering the salary column.Eliminating duplicates in nameimport?pandas?as?pddata?=?{'name':?['Alice',?'Alice',?'Charles',?'David',?'Eric'],????????'year':?[2017,?2016,?2017,?2018,?2016],????????'salary':?[40000,?24000,?31000,?20000,?30000]}?df?=?pd.DataFrame(data,?index?=?['Acme',?'Acme',?'Bilbao',?'Bilbao',?'Bilbao'])?print(df)print('----------')?df_filtered?=?df.query('salary>30000')print(df_filtered)print('Sorting')dfsorted=df.sort_values('salary')print(dfsorted)print('Eliminate?duplicates')dfdistinct=df.drop_duplicates(subset='name',keep='last')print(dfdistinct)CSVWrite a python that performs the following operationsStore the result of n students in a csv file.Display the contents in the file after reading them to a dataframe.import pandas as pdl=[]n=[]math=[]phy=[]tot=[]fori in range(5):name=input('Enter name:') m1=int(input('Mark of maths:')) m2=int(input('Mark of Science:'))math.append(m1)phy.append(m2)n.append(name)tot.append(m1+m2)print('The dataframe created')dict1={'MATHS':pd.Series(math,index=n),'PHY':pd.Series(phy,index=n)}df= pd.DataFrame(dict1)df.to _csv(‘RESULTFILE.CSV’)df1=pd.read_csv(‘RESULTFILE.CSV’)print(‘The contents in the file’)print(df1) ................
................

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

Google Online Preview   Download