Programs Using Pandas

Q Write a program to create dataframe for 3 student including name and roll numbers. and add new columns for 5 subjects and 1 column to calculate percentage. It should include random numbers in marks of all subjects

import pandas as pd, numpy as np, random
D={‘Roll’:[1,2,3],’Name’:[‘Sangeeta’,’Shanti’,’Swati’]}
P=[]
C=[]
M=[]
E=[]
H=[]
SD=pd.DataFrame(D)
for i in range(3):
P.append(random.randint(1,101))
C.append(random.randint(1,101))
M.append(random.randint(1,101))
E.append(random.randint(1,101))
H.append(random.randint(1,101))
SD[‘Phy’]=P
SD[‘Chem’]=C
SD[‘Maths’]=M
SD[‘Eng’]=E
SD[‘Hin’]=HSD

[‘Total’]=SD.Phy+SD.Chem+SD.Maths+SD.Eng+SD.Hin
SD[‘Per’]=SD.Total/5

print(SD)

2. to concat DataFrames

import pandas as pd
df1 = pd.DataFrame({‘9A’: [‘Shreya’, ‘Akash’, ‘Rita’, ‘Priya’],
‘9B’: [‘Manasvi’, ‘Kavita’, ‘Reena’, ‘Radha’],
‘9C’: [‘Shruti’, ‘Riya’, ‘Ravina’, ‘Harsh’], },
index = [0, 1, 2,3])
df2 = pd.DataFrame({‘9A’: [‘Akshat’, ‘Akansha’, ‘Mohit’, ‘Piyush’],
’10B’: [‘Harshal’, ‘Savita’, ‘Meena’, ‘Anuradha’],
‘9C’: [‘Anukriti’, ‘Niya’, ‘Vina’, ‘Mona’], },
index = [0, 1, 2,3])
print(df1)
print(df2)
print(‘—-inner(INTERSECTION)—–‘)
print(pd.concat([df1,df2],join=’inner’))
print(‘—-outer(UNION)—–‘)
print(pd.concat([df1,df2]))
Key_wise=pd.concat([df1,df2],keys=[‘K1′,’K2′],join=’inner’)# each key for 1 dataframeresult
print(Key_wise)
print(Key_wise.loc[‘K2’])