Type C: Practical/Knowledge-Based Questions
5. Given a data frame dfl as shown below:
1990 2000 2010
a 52 340 890
b 64 480 560
c 78 688 1102
d 94 766 889
Write code to create:
(a) A scatter chart from the 1990 and 2010 columns of dataframe dfl .
(b) A line chart from the 1990 and 2000 columns of dataframe dfl .
(c) Create a bar chart plotting the three columns of dataframe dfl .
Ans:
import pandas as pd
import matplotlib.pyplot as plt
# Creating Dictionary and DataFrame
data = {‘1990’ : [52,64,78,94],
‘2000’ : [340,480,688,766],
‘2010’ : [890,560,1102,889]}
df1 = pd.DataFrame(data, index=[‘a’, ‘b’, ‘c’,’d’])
print(df1)
# 5(a)
plt.scatter(x = df1.index, y = df1[‘1990’])
plt.scatter(x = df1.index, y = df1[‘2010’])
plt.show()
#5 (b)
plt.plot(df1.index, df1[‘1990’], df1[‘2000’])
plt.show()
# 5(c)
df1.plot(kind=’bar’)
plt.show()
6. The score of four teams in 5 IPL matches is available to you. Write a program to plot these in a bar charts.
Ans:
import pandas as pd
import matplotlib.pyplot as plt
IPL = {‘CSK’:[250,350,216,315,278],
‘RR’:[298,247,231,264,198],
‘DDW’:[310,254,279,249,233],
‘RCB’:[216,239,258,365,347]}
dfIPL = pd.DataFrame(IPL, index=range(1,6))
print(dfIPL)
dfIPL.plot(kind=’bar’)
plt.title(“IPL Match Summary”)
plt.xlabel(“Match Number”)
plt.ylabel(“Score (Runs)”)
plt.show()