KEMBAR78
Python Pandas Exercises for Students | PDF | Computer Programming
0% found this document useful (0 votes)
41 views6 pages

Python Pandas Exercises for Students

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)
41 views6 pages

Python Pandas Exercises for Students

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/ 6

Shree Krishna International School, Vapi

Plot No. 12, Charwada Road, Gunjan Road, Vapi-396151 Dist.-Valsad, Gujarat.
Std -12
Chapter -1 (Pandas-I)
Exercise- Type- C Solutions
Q.1 Q. Write Python code to create a Series object Temp1 that stores temperatures of seven
days in it. Take any random seven temperatures.
Answer =
import pandas as pd
data = [20,30,25,35,40,39,28]
Temp1 = pd.Series(data)
print(Temp1)

Q. 2 Write Python code to create a Series object Temp2 storing temperatures of seven days of
week. Its indexes should be ‘Sunday'. Monday'. ‘Saturday’.
Answer =
import pandas as pd
data = [20, 30, 25, 35, 40, 39, 28]

Temp1 =
pd.Series(data,index=['monday','tuesday','Wednesday','Thursday','Friday','Saturday'
,'Sunday '])

print(Temp1)

Q. 3 A series object (say T1) stores the average temperature recorded on each day of a month.
Write code to display the temperatures recorded on:
(i) First 7 days
(ii) Last 7 days.

Answer =
import pandas as pd
T1=pd.Series([25,27,32,39,22,18,20,31,29,40,17,12,8,18,23,26,27,32,31,33,17,12,8,18
,23,26,27,32,31,33])

print(T1)

print("First 7 days Temp")


print(T1.head(7))

print("Last 7 days Temp")


print(T1.tail(7))

Q. 4 Series objects Temp1, Temp2, Temp3, Temp4 store the temperatures of days of week1,
week2, week3, weeken4 respectively. Write a script to

(a) Print the average temperature per week


(b) Print average temperature of entire month.

Answer =
#(a)

import pandas as pd
import numpy as np

t=[25,27,32,39,22,18,20,31,29,40,17,12,8,18,23,26,27,32,31,33,17,12,8,18,23,26,27,3
2,31,33]
print(t)

Temp1=pd.Series(t[0:7])
print("Temperature Week-1")
print(Temp1)

Temp2=pd.Series(t[7:14])
print("Temperature Week-2")
print(Temp2)

Temp3=pd.Series(t[14:21])
print("Temperature Week-3")
print(Temp3)

Temp4=pd.Series(t[21:])
print("Temperature Week-4")
print(Temp4)

#(b)

import pandas as pd
import numpy as np

t=[25,27,32,39,22,18,20,31,29,40,17,12,8,18,23,26,27,32,31,33,17,12,8,18,23,26,27,3
2,31,33]
print(t)

av1=Temp1.sum()/7
print("Average Temperature of Week-1",av1)

av2=Temp2.sum()/7
print("Average Temperature of Week-2",av2)

av3=Temp3.sum()/7
print("Average Temperature of Week-3",av3)

av4=Temp4.sum()/7
print("Average Temperature of Week-4",av4)

mav=(av1+av2+av3+av4)/4
print("Average Temperature of Month-",mav)

Q. 7 Three Series objects store the marks of 10 students in three terms. Roll numbers of
students from the index of these Series objects. The Three Series objects have the same
indexes.
Calculate the total weighted marks obtained by students as per following formula:

Final marks = 25%. Term 1 + 25% Term 2 + 50% Term 3

Store the Final marks of students in another Series object.

Answer =
import pandas as pd

Term1 = pd.Series([40,35,25,20,22,26,28,29,23,28])
Term2 = pd.Series([28,15,37,38,45,41,48,47,30,20])
Term3 = pd.Series([36,23,34,31,21,22,23,24,26,28])
final_mark = (Term1*25)/100 + (Term2*25)/100 + (Term3*50)/100

print(final_mark)

Q.8 Write code to print all the information about a Series object.
Answer =
import pandas as pd
s1=pd.Series([12,18,21,26,32,39,41,56])
print(s1)

print(s1.index)
print(s1.index.name)
print(s1.values)
print(s1.dtype)
print(s1.shape)
print(s1.size)
print(s1.nbytes)
print(s1.ndim)
print(s1.itemsize)
print(s1.hasnans)
print(s1.empty)
print(s1.name)

OR
import pandas as pd
s = pd.Series([40,35,25,20,22,26,28,29,23,28])

print("shape of Series :", s.shape)


print("dtype of Series :", s.dtype)
print("index of Series :", s.index)
print("nbytes of Series :", s.nbytes)
print("Ndimension of Series :", s.ndim)
print("size of Series :", s.size)
print("NaN values of Series :", s.hasnans)

. 9 Write a program to create three different Series objects from the three columns of a
DataFrame df.
Answer =
import pandas as pd

dict={'Name':['Sneha','Aryan','Sai','Siddhi','Arya'],
'Age':[17,16,18,17,17],
'Blood_Group':['AB+','B+','A-','O+','A+']
}
df=pd.DataFrame(dict)
print(df)

name=pd.Series(df['Name'])
print(name)

age=pd.Series(df['Age'])
print(age)

blood_group=pd.Series(df['Blood_Group'])

print(blood_group)

OR
import pandas as pd

d = {"col1":[1,4,3],"\
col2":[6,7,8],'\
col3':[9,0,1]}

df = pd.DataFrame(d)
s = pd.Series(data=df[df.columns[0]])
s1 = pd.Series(data=df[df.columns[1]])
s2 = pd.Series(data=df[df.columns[2]])

OR
import pandas as pd
dict={'Name':['Ruchi','Vrushti','Shivam','Nishant','Aditi'],
'Age':[16,16,17,16,17],
'Blood_Group':['AB+','O-','B+','AB+','O+']}

df=pd.DataFrame(dict)
print(df)

Name=pd.Series(df.Name)
print("Name List")
print(Name)

Age=pd.Series(df.Age)
print("Age")
print(Age)
Blood_Group=pd.Series(df.Blood_Group)
print("Blood Group")
print(Blood_Group)
Q. 10 Write a program to create three different Series objects from the three rows of a
DataFrame df.
Answer =
import pandas as pd

dict={'Name':['Sneha','Aryan','Sai','Siddhi','Arya'],
'Age':[17,16,18,17,17],
'Blood_Group':['AB+','B+','A-','O+','A+']
}
df=pd.DataFrame(dict)
print(df)

row1=pd.Series(df.iloc[0])
print(row1)

row2=pd.Series(df.iloc[1])
print(row2)

row3=pd.Series(df.iloc[3])
print(row3)

Q. 11 Write a program to create a Series object from an ndarray that stores characters from 'a'
to 'g'.
Answer =
import pandas as pd
import numpy as np

arr = np.array(list('abcdef'))
print(arr)
s1 = pd.Series(arr)
print(s1)

Q. 12 Write a program to create a Series object that stores the table of number 5.

Answer =

import pandas as pd
s2=pd.Series(range(5,55,5))

print(s2)
OR
import pandas as pd
import numpy as np
tab = np.arange(5,55,5)
ser = pd.Series(tab)
print(ser)
Q. 13 Write a program to create a DataFrame that stores two columns, which store the Series
objects of the previous two questions (10 and 11).
Answer =
import pandas as pd
import numpy as np

s=np.array(list('abcdefg'))
s1=pd.Series(s)
print(s1)

s2=pd.Series(range(5,55,5))
print(s2)

df=pd.DataFrame({'Col1':s1,'Col2':s2})
print(df)

Q. 14 Write a program to create a DataFrame storing salesmen details (name, zone, sales) of five
salesmen.
Answer =
import pandas as pd
import numpy as np
d = {"name":['name1','name2','name30,','name4','name5'],"\
zone":['zone1','zone2','zone3','zone4','zone5'],"sales":[100,200,150,350,250
]}

df = pd.DataFrame(d)
print(df)

Q. 15 Four dictionaries store the details of four employees-of-the-month as (empno, name).


Write a program to create a DataFrame from these.
Answer =
import pandas as pd
import numpy as np

d = {"name":'name',"empno":1}
d1 = {"name":'name1',"empno":2}
d2 = {"name":'name2',"empno":3}
d3 = {"name":'name3',"empno":4}
s = pd.Series([d,d1,d2,d3])

Q. 16 A list stores three dictionaries each storing details, (old price, new price, change). Write a
program to create a DataFrame from it.
Answer =
import pandas as pd
import numpy as np

list = [{"old price":[10,20,30,50,40]},{"new


price":[15,20,25,36,50]},{"change":[5,0,-5,-14,10]}]
df = pd.DataFrame(list[0])
df["new price"] = list[1]['new price']
df["change"] = list[2]["change"]
print(df)

You might also like