0% found this document useful (0 votes)
9 views13 pages

Python Data Analysis with Pandas

Uploaded by

ishaanjoby67
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)
9 views13 pages

Python Data Analysis with Pandas

Uploaded by

ishaanjoby67
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

PROGRAMS USING PYTHON PANDAS AND

MATPLOTLIB (Questions with Answers)

Q.1 Create a Dataframe ‘Weather Forecast’ to store details of weather as


given below.
Day Temperature Windspeed
01/03/2021 41 6
02/03/2021 43 7
03/03/2021 40 4
04/03/2021 39 5

Source Code:
import pandas as pd
dict_weather={"Day":['01/03/2021','02/03/2021','03/03/2021','04/03/2021'],"Temperature":[
41,43,40,39],"Windspeed":[6,7,4,5]}
Weather_Forecast=[Link](dict_weather)
print(Weather_Forecast)
print()
print("To view the first two rows of the DF")
print(Weather_Forecast.head(2))
print()
print("To view the 2nd & third rows in the DF")
print(Weather_Forecast[1:3])
print()
print("To view only the 'Day' column in the DF")
print(Weather_Forecast.Day)
print()
print("To view the maximum Temperature in the DF")
print(Weather_Forecast["Temperature"].max())
print()
print("To sort the table in the descending order of the Temperature")
print(Weather_Forecast.sort_values(by=["Temperature"],ascending=False))
Output:

Day Temperature Windspeed


0 01/03/2021 41 6
1 02/03/2021 43 7
2 03/03/2021 40 4
3 04/03/2021 39 5

To view the first two rows of the DF


Day Temperature Windspeed
0 01/03/2021 41 6
1 02/03/2021 43 7

To view the 2nd & third rows in the DF


Day Temperature Windspeed
1 02/03/2021 43 7
2 03/03/2021 40 4

To view only the 'Day' column in the DF


0 01/03/2021
1 02/03/2021
2 03/03/2021
3 04/03/2021
Name: Day, dtype: object

To view the maximum Temperature in the DF


43

To sort the table in the descending order of the Temperature


Day Temperature Windspeed
1 02/03/2021 43 7
0 01/03/2021 41 6
2 03/03/2021 40 4
3 04/03/2021 39 5

Q.2 Create two DataFrames Df1 for Price1 & Df2 for Price2 to store the
price of 4 items & perform the following operations.
a) Add Df1 & Df2
b) Subtract Df1 from df2
c) Change the index label of Df1 as ‘zero’,’one’,’two’,’three’, four’.
d) Rename column as Cost1 & Cost2

Source Code:
import pandas as pd
Df1=[Link]({"Price1":[1050,5299,560,765],"Price2":[670,540,550,899]})
Df2=[Link]({"Price1":[200,5299,300,750,],"Price2":[110,950,350,85]})
print(Df1)
print()
print(Df2)
print()
print("To add Df1 & Df2")
print([Link](Df2))
print("To subtract Df1 from Df2")
print([Link](Df2))
print("To change the index label of Df1 as 'zero','one,'two','three'")
[Link]=["Zero","One","Two","Three"]
print(Df1)
print("To rename the columns as Cost1 & Cost2")
[Link](columns={"Price1":"Cost1","Price2":"Cost2"},inplace=True)
print(Df1)

Output:

Price1 Price2
0 1050 670
1 5299 540
2 560 550
3 765 899

Price1 Price2
0 200 110
1 5299 950
2 300 350
3 750 85

To add Df1 & Df2


Price1 Price2
0 1250 780
1 10598 1490
2 860 900
3 1515 984
To subtract Df1 from Df2
Price1 Price2
0 850 560
1 0 -410
2 260 200
3 15 814
To change the index label of Df1 as 'zero','one,'two','three'
Price1 Price2
Zero 1050 670
One 5299 540
Two 560 550
Three 765 899

To rename the columns as Cost1 & Cost2


Cost1 Cost2
Zero 1050 670
One 5299 540
Two 560 550
Three 765 899

Q.3 Create a DataFrame ‘ClassDetails’ to store details of 3 classes as


given below.

Class Teacher Strength


12A [Link] 35
12B [Link] 36
12C [Link] 38

a) Change the index of DataFrame as class.


b) Reset the index of DataFrame to default.
c) View the indices, columns, axes, size & shape of the DataFrame.

Source Code:
import pandas as pd
ClassDetails=[Link]({"Class":['12A','12B','12C'],"Teacher":["[Link]","[Link]","
[Link]"],"Strength":[35,36,38]})
print(ClassDetails)
print()
print("Changing the index of DF to Class")
ClassDetails.set_index("Class",inplace=True)
print(ClassDetails)
print()
print("Resetting the index of DF to default")
ClassDetails.reset_index(inplace=True)
print(ClassDetails)
print()
print("Indices:",[Link])
print("Columns:",[Link])
print("Size:",[Link])
print("Shape:",[Link])
Output:
Class Teacher Strength
0 12A [Link] 35
1 12B [Link] 36
2 12C [Link] 38

Changing the index of DF to Class


Teacher Strength
Class
12A [Link] 35
12B [Link] 36
12C [Link] 38

Resetting the index of DF to default


Class Teacher Strength
0 12A [Link] 35
1 12B [Link] 36
2 12C [Link] 38

Indices: RangeIndex(start=0, stop=3, step=1)


Columns: [RangeIndex(start=0, stop=3, step=1), Index(['Class', 'Teacher',
'Strength'], dtype='object')]
Size: 9
Shape: (3, 3)

Q.4 Create a series ‘Marks’ to find the percentage of marks obtained by


5 students out of 25 accepted from a NumPy ndarray. The marks are
18,23,20,25 and 24. The indices should be from RollNo:1 to RollNo:5.
Source Code:
import pandas as pd
import numpy as np
st_marks=[Link]([18,23,20,25,24])
Marks=[Link](st_marks/25*100,index=["RollNo:1","RollNo:2","RollNo:3","RollNo:4","Roll
No:5"])
print(Marks)

Output:
RollNo:1 72.0
RollNo:2 92.0
RollNo:3 80.0
RollNo:4 100.0
RollNo:5 96.0

Q.5 Create a series to store the temperature of 8 cities & to find the
hottest & coldest city.

Source Code:
import pandas as pd
Cities=["Calicut","Chennai","Bangalore","Kashmir","Manali","Delhi", "Ahmedabad","Jaipur"]
Temperature=[28,29,23,5,11,26,27,25]
CT1=[Link](Temperature,index=Cities)
print(CT1)
print()
print("The hottest city:")
print(CT1.sort_values(ascending=False).head(1))
print()
print("The coldest city:")
print(CT1.sort_values(ascending=True).head(1))

Output:

Calicut 28
Chennai 29
Bangalore 23
Kashmir 5
Manali 11
Delhi 26
Ahmedabad 27
Jaipur 25
dtype: int64

The hottest city:


Chennai 29
dtype: int64

The coldest city:


Kashmir 5
dtype: int64

Q.6 Create two series S1 & S2 to perform mathematical operations such


as add, subtract, multiply and division. Also show that aruthmetic
operation is possible only with series having same indices by performing
operation on another series S3 with different indices.

Source Code:
import pandas as pd
S1=[Link]([100,200,300,400],index=['a','b','c','d'])
S2=[Link]([10,20,30,40],index=['a','b','c','d'])
S3=[Link]([10,20,30,40],index=['a','b','f','d'])
print(S1)
print()
print(S2)
print()
print(S3)
print()
print("Sum of Series:")
print(S1+S2)
print()
print("Difference between Series:")
print(S1-S2)
print()
print("Product of Series:")
print(S1*S2)
print()
print("Dividing Series:")
print(S1/S2)
print()
print(S1+S3)
print("S1 & S3 have different indices so mathematical operation cannot be performed & give
NaN values.")

Output:
a 100
b 200
c 300
d 400
dtype: int64

a 10
b 20
c 30
d 40
dtype: int64

a 10
b 20
f 30
d 40
dtype: int64

Sum of Series:
a 110
b 220
c 330
d 440
dtype: int64
Difference between Series:
a 90
b 180
c 270
d 360
dtype: int64

Product of Series:
a 1000
b 4000
c 9000
d 16000
dtype: int64

Dividing Series:
a 10.0
b 10.0
c 10.0
d 10.0
dtype: float64

a 110.0
b 220.0
c NaN
d 440.0
f NaN
dtype: float64
S1 & S3 have different indices so mathematical operation cannot be
performed & give NaN values.
Q.7 Create python application to analyse the performance of two cricket
players over years using line chart.

Source Code:

import [Link] as plt


years=[1992,1996,2000,2004]
per1=[9,10,9.5,10]
per2=[8,9,9.5,10]
[Link]("Batting Performance")
[Link]("Year")
[Link](years,per1,'m',linestyle='-',linewidth=2,marker='*',markersize=10,markeredgecolor='r',l
abel="Sachin")
[Link](years,per2,'k',linestyle=":",linewidth=2,marker='+',markersize=8,markeredgecolor="g",
label="Ganguli")
[Link]()
[Link]()

Output:
Q.8 Create a python application to analyse the blood sugar level in men
& women using histogram.

Source Code:
import [Link] as plt
blood_sugar_men=[133,90,149,93,115,80,82]
blood_sugar_women=[98,120,150,69,120,100,79]
[Link]("Sugar range")
[Link]("Total no. of patients")
[Link]("Blood Sugar Analysis")
[Link]([blood_sugar_men,blood_sugar_women],bins=[80,100,125,150],rwidth=0.95,color=['y
ellow','blue'],label=['men','women'])
[Link]()
[Link]()

Output:
Q.9 Create a bar graph to compare the marks of 5 students

Source Code:

import [Link] as plt


Name=["Aaryan","Akshara","Akshay","Ananya","Devika"]
Marks=[91,95,90,89,92]
[Link](True)
[Link](Name,Marks,width=0.5,color='g',label="Marks")
[Link]("Name of student")
[Link]("Marks")
[Link]("MARKS ANALYSIS")
[Link]()
[Link]()

Output:

You might also like