0% found this document useful (0 votes)
44 views4 pages

Preeti Arora Solution1

The document shows code to perform various operations on pandas DataFrames like creating DataFrames from dictionaries, adding/inserting rows and columns, displaying specific rows/columns, concatenating DataFrames, checking for empty/null values, and applying any/all functions. It contains examples of creating DataFrames, printing them, adding/inserting data, selecting subsets of data, concatenating DataFrames, and using pandas functions like fillna, any, all etc on the DataFrames.

Uploaded by

Pain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views4 pages

Preeti Arora Solution1

The document shows code to perform various operations on pandas DataFrames like creating DataFrames from dictionaries, adding/inserting rows and columns, displaying specific rows/columns, concatenating DataFrames, checking for empty/null values, and applying any/all functions. It contains examples of creating DataFrames, printing them, adding/inserting data, selecting subsets of data, concatenating DataFrames, and using pandas functions like fillna, any, all etc on the DataFrames.

Uploaded by

Pain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Ans 29:

import pandas as pd
Pdict={'Enrolment_No':[101,102,103,104],
'Name':['Rekha','Divya','Geet','Jeet'],
'Class':['XII','XII','XII','XII'], 'Section':['B','C','H','B'],
'Project_Name':['Data Analysis','Graphical Analysis','Machine Learning','App Development']}
dfProject=pd.DataFrame(Pdict)
print(dfProject)
print("Ans (i)")
print("Add record using loc ")
dfProject.loc[4]=[105,'Aviral','XII','B','Graphical Analysis']
print(dfProject)
print("Add record using append")
new_row={'Enrolment_No': 106,'Name':'Jyotsna','Class':'XII','Section':'H','Project_Name':'App
Development'}
dfProject=dfProject.append(new_row,ignore_index=True)
print(dfProject)

print("Ans (ii): Insert column to store Grade : ")


dfProject['Grade']=['A','B','A+','B+','C','A+']
print("Ans (iii)")
print("Display Name and Section")
print(dfProject.loc[:,['Name','Section']]) # or print(dfProject.iloc[:,[1,3]])
print('Ans (iv)')
print("Display record with index value 101 and 102")
print(dfProject.loc[[0,1],:]) # or dfProject.iloc[[0,1],:] or dfProject.head(2)
print('Ans (v)')
print('Insert column after name to store school name')
SName=["DIS",'DPS','DPVN','PCVN','SPSEC','SDEC']
print(dfProject.insert(loc=2,column='School_Name',value=SName))
print(dfProject)
print('Ans (vi)')
print("Display second and third record ")
print(dfProject[1:3]) #or print(dfProject.iloc[1:3]) or print(dfProject.loc[1:2])
print("Ans (vii)")
print("Replace name and sectionof jeet to 'XI', 'A' ")
dfProject.iat[3,3]='XI'
dfProject.iat[3,4]='A'
print(dfProject)
dfProject.drop(['Project_Name','Section'],axis=1,inplace=True)
print(dfProject)
Ans 30:
import pandas as pd
Cdict={'ID': [100,110,120,130],'State':['Delhi', 'Mumbai', 'Chennai','Suara'],
'Cases':[3000,4000,5000,4500]}
coronaDf=pd.DataFrame(Cdict)
print(coronaDf)
coronaDf['Recovery']=pd.Series([657643,347689,326589,894533])
print(coronaDf)
coronaDf=coronaDf.assign(Death=[546,875,278,895])
print(coronaDf)
coronaDf.loc[4]=[140, 'Kolkata',4300,67453456,200]
print(coronaDf)
coronaDf.insert(loc=3,column='Percentage', value=[45,77,32,78,12])
print(coronaDf)
del coronaDf['Percentage']
print(coronaDf)
coronaDf.pop('Death')
print(coronaDf)
coronaDf.iloc[0]=[150,'Kanpur',2000, 453]
print(coronaDf)
print(coronaDf.drop(['Cases','State'],axis=1))
Ans 31:
import pandas as pd
Nm=pd.Series(['Riya','Kirti','Nandini','Devansh','Shashwat'])
gd=pd.Series(['A','B','A+','C','A'])
mks=pd.Series([76,98,54,76,54])
std={'Name':Nm,'Grade':gd,'Marks':mks}
studentDf=pd.DataFrame(std)
print(studentDf)
print('Display first three records')
print(studentDf.head(3))
print('Display last two records')
print(studentDf.tail(2))
Ans 32:
import pandas as pd
aDict={'Name':['Nancy','Riya','Nandini','Kirti','Mansi'],
'Sub1':[54,76,98,34,87],'Sub2':[86,53,79,24,86],'Sub3':[43,76,95,43,76],
'Sub4':[54,76,98,34,87],'Sub5':[76,54,98,34,65]}
stdDf=pd.DataFrame(aDict)
print(stdDf)
print('Display the first 5 rows of student \n', stdDf.head())
print("Display the bottom 3 rows of student\n",stdDf.tail(3))
Ans 33:
import pandas as pd
aDict={'Name':['Nancy','Riya','Nandini','Kirti','Mansi'],
'Salary':[54000,76000,98000,34000,87000]}
empDf1=pd.DataFrame(aDict)
print(empDf1)

empDf1.Salary=empDf1.Salary+5000
print('-'*40)
print('Add bonus 5000')
print(empDf1)
aDict1={'Name':['Alok','Vishal','Shaswat','Ravi','Manisha'],
'Salary':[14000,33000,28000,64000,83000]}
empDf2=pd.DataFrame(aDict1)
print('-'*40)
print(empDf2)

empDf2.Salary=empDf2.Salary+5000
print('-'*40)
print('Add bonus 5000')
print(empDf2)

Ans 34:
import pandas as pd
lst=[[10,11,12,13,14],[23,34,45,32,65],[55,60,65,70,75]]
df=pd.DataFrame(lst)
print(df)
df.loc[3]=[1,2,3,4,5]
print(df)
Ans 35 :
import pandas as pd
lst=[[23,25],[34],[43,44,45,46]]
df=pd.DataFrame(lst)
print(df)
print()
print(df.fillna(0))
print()
print(df.fillna({0:-1,1:-2,2:-3,3:-4}))
print()
print(df.fillna(method='ffill'))
Ans 36:
import pandas as pd
D1={'Rollno':[1001,1004,1005,1008,1009],'Name':['Sarika','Abhay','Mohit','Ruby','Govind']}
D2={'Rollno':[1002,1003,1004,1005,1006],'Name':['Seema','Jai','Shweta','Sonia','Nishant']}
df1=pd.DataFrame(D1)
df2=pd.DataFrame(D2)
df3=pd.concat([df1,df2],axis=0)
print('concatenate row-wise\n',df3)
df4=pd.concat([df1,df2],axis=1)
print('concatenate column-wise\n',df)
Ans 37:
import pandas as pd
aDict={'A':[]}
df=pd.DataFrame(aDict)
print(df)
print(df.empty)
Ans 38:
import pandas as pd
aDict={'A':[5,6],'B':[3,0],'C':[0,0]}
df=pd.DataFrame(aDict)
print(df)
result=df.any()
print(result)
result=df.all()
print(result)
Ans 39:
import pandas as pd
aDict={'A':[True,True],'B':[True,False],'C':[False,False]}
df=pd.DataFrame(aDict)
print(df)
result=df.any()
print(result)
result=df.all()
print(result)

You might also like