0% found this document useful (0 votes)
249 views22 pages

Literacy Rate Analysis Coding and Output

This document contains code for analyzing literacy rates in India using Pandas and Matplotlib. It includes functions for data visualization, analysis, and manipulation. The main menu allows selecting these options to explore a CSV dataset on literacy rates by state from 1951 to 2011.

Uploaded by

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

Literacy Rate Analysis Coding and Output

This document contains code for analyzing literacy rates in India using Pandas and Matplotlib. It includes functions for data visualization, analysis, and manipulation. The main menu allows selecting these options to explore a CSV dataset on literacy rates by state from 1951 to 2011.

Uploaded by

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

LITERACY RATE ANALYSIS

SYSTEM OF INDIA
CODING :
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sys
import mysql.connector as conn
import time

global df
df=pd.read_csv("literacy_rate.csv")
#---------------------------------------------
#Function to display the main menu.
#---------------------------------------------
def MainMenu():
ans='y'
while ans=='y' or ans=='Y':
opt=""
print()
print("===============================================")
print(" Literacy Rate Of India ")
print("***********************************************")
print("1-Data Visualisation\n")
print("2-Analysis\n")
print("3-Manipulation\n")
print("4-Exit")
print("===============================================")
opt=input("Enter your choice: ")
if opt=='1':
visuals()
elif opt=='2':
analysis()
elif opt=='3':
manipulation()
elif opt=='4':
my_chance=input("Do you really want to exit?(y/n)")
if my_chance=="y" or my_chance=="Y":
print("Thank you. Exiting now......")
sys.exit()
else:
print("\nInvlaid choice. Try again")
continue
else:
ans=input("Do you want to continue(y/n)")

#------------------------------------------------
#Function for Data Visualization.
#------------------------------------------------
def visuals():
df=pd.read_csv("literacy_rate.csv")
while True:
print("V I S U A L M E N U")
print("=====================")
print("1-Line Chart of a Particular Year")
print("2-Bar Chart for different Years")
print("3-Histogram for a Year")
print("4-Line Chart Average Literacy Year wise")
print("5-Back to Main Menu")
print("==================================")
choice=int(input("Enter your choice: "))
if choice==1:
year=input("Enter Year of Literacy : ")
df1=df.sort_values(by=[year],ascending=False)
n=int(input("Enter number of states (1-34) : "))
print(df1.head(n))
df1.plot(x ='State/ UTs', y=year, kind = 'line',rot=75)
plt.xlabel("State/UT Name-->", color='b',fontsize=12)
plt.ylabel("Literacy Rate (%)->", color='b',fontsize=12)
plt.title("Top "+str(n)+" Indian State(s) Literacy Analysis",color='r',fontsize=16)
plt.show()
elif choice==2:
year=eval(input("Enter Year of Literacy as list like['1951','2011']: "))
n=int(input("Enter number of states (1-34) : "))
print(df.head(n))
df1=df.head(n)
df1.plot(x='State/ UTs',y=year,kind='bar',rot=75)
plt.xlabel("State/UT Name-->", color='r',fontsize=12)
plt.ylabel("Literacy Rate (%)->", color='r',fontsize=12)
plt.title("Top "+str(n)+" Indian State(s) Literacy Analysis",color='g',fontsize=16)
plt.show()
elif choice==3:
year=input("Enter Year of Literacy : ")
df.hist(column=year,color='r',edgecolor='black')
plt.xlabel("Literacy Rate (%)->", color='r',fontsize=12)
plt.ylabel("No. of States", color='r',fontsize=12)
plt.title("All Indian State(s) Literacy Analysis of \nYear-"+year,color='g',fontsize=16)
plt.show()
elif choice==4:
s1=df.mean()
mylabel=s1.index
plt.plot(mylabel,s1.values,linestyle='dashed',linewidth=3,\
color='r',marker='o',mfc='b',ms=10)
plt.xlabel("Year-->", color='b',fontsize=12)
plt.ylabel("Literacy Rate (%) of India ->", color='b',fontsize=12)
plt.title("Indian Literacy Rate Analysis 1951 to 2011", color='r',fontsize=16)
plt.grid()
plt.show()
else:
break

#------------------------------------------------
#Function to analyse data from a dataframe.
#------------------------------------------------
def analysis():
df=pd.read_csv("literacy_rate.csv")
while True:
print("Data Frame Analysis")
print("*******************")
menu='''\n 1.Top record
\n 2.Bottom records
\n 3.To Display Literacy of a particular year
\n 4.To Display States with Literacy rate is >= n% in a year
\n 5.To Display States with Maximun Literacy rate
\n 6.To display Average Literacy of India
\n 7.To display Complete DataFrame
\n 8.To Minimum, Maximum and Average Literacy in a year"
\n 9.Back to Main Menu'''
print(menu)
print("==========================================")
ch_an=int(input("Enter your choice: "))
if ch_an==1:
n=int(input("Enter the number of records to be displayed: "))
print("Top",n,"records from the dtaframe")
print(df.head(n))
elif ch_an==2:
n=int(input("Enter the number of records to be displayed: "))
print("Bottom",n,"records from the dtaframe")
print(df.tail(n))
elif ch_an==3:
print("Name of the columns\n",df.columns)
col=eval(input("Enter the year of Literacy like ['State/ UTs','1951']: "))
print(df.loc[:,col])
elif ch_an==4:
yr=input("Enter Year : ")
n=float(input("Enter percentage :"))
df1=df.loc[(df[yr]>n),['State/ UTs',yr]]
df1=df1.sort_values(by=yr,ascending=False)
print(df1)
elif ch_an==5:
yr=input("Enter Year : ")
print()
print("State with Maximum Literacy in the year-"+yr)
print("----------------------------------------------")
x=df[yr].max()
print(df.loc[(df[yr]==x),['State/ UTs',yr]])
print("----------------------------------------------\n")
elif ch_an==6:
print("Average Literacy of India")
print("-------------------------")
print(df.mean())
print("-------------------------")
elif ch_an==7:
print("Displaying Complete DataFrame")
print("-------------------------")
print(df)
print("-------------------------")
elif ch_an==8:
yr=input("Enter Year : ")
print("\nMaximum Literacy:")
print("-------------------------")
x=df[yr].max()
print(df.loc[(df[yr]==x),['State/ UTs',yr]])
print("-------------------------")
print("Minimum Literacy:")
print("-------------------------")
y=df[yr].min()
print(df.loc[(df[yr]==y),['State/ UTs',yr]])
avg=df[yr].mean()
print("------------------------------------------------------------")
print("Average Literacy in the Year-"+yr+" in India =",avg)
print("------------------------------------------------------------")
else:
break

#------------------------------------------------
#Function to manipulate data in a dataframe.
#------------------------------------------------
def manipulation():
df=pd.read_csv("literacy_rate.csv")
while True:
print("\n\nManipulation Menu")
print("*********************")
print('''\n1. Insert a row\n
2. Insert a Column\n
3. Delete a Row\n
4. Delete a column\n
5. Update a cell value\n
6. Back to Main Menu''')
print("=====================")
mch=int(input("Ente your choice: "))
if mch==1:
df1=pd.DataFrame()
col=df.columns
print(col)
print(df.head(1))
j=0
lst=[]
lst1=eval(input("Enter a list of values in the sequence of columns: "))
print(lst1)
s1=pd.Series(lst1,index=df.columns)
df1=df.append(s1,ignore_index=True)
print("New row inserted")
print(df1)
df1.to_csv("literacy_rate.csv",index=False)
df=pd.read_csv("literacy_rate.csv")
elif mch==2:
n=int(input("How Many States? "))
yr=input("Enter Year of Literacy : ")
lst_literacy=[]
for i in range(n):
lrate=float(input("Enter literacy rate of "+df.loc[i,['State/ UTs']]+": "))
lst_literacy.append(lrate)
df[yr]=lst_literacy
print(df)
df.to_csv("literacy_rate.csv",index=False)
df=pd.read_csv("literacy_rate.csv")
elif mch==3:
print("List of States-\n",df['State/ UTs'])
n=int(input("Enter the index number of the State for row deletion :"))
ch=input("Do you really want to delete row of-\n"+str(df[(df.index==n)])+"(y/n)?")
if ch=='y' or ch=='Y':
df.drop(index=n,inplace=True)
print(df)
print("Row of index no- ",n,"deleted successfully!!!")
df.to_csv("literacy_rate.csv",index=False)
df=pd.read_csv("literacy_rate.csv")
elif mch==4:
print(df.columns)
col=input("Enter column name to be deleted from the above")
ch=input("Do you really want to delete column(y/n)?")
if ch=='y' or ch=='Y':
del df[col]
print("Column- ",col,"deleted successfully!!!")
print(df)
df.to_csv("literacy_rate.csv",index=False)
df=pd.read_csv("literacy_rate.csv")
elif mch==5:
print(df)
row_idx=int(input("Enter row index/label for edit : "))
col_idx=input("Enter column index/label for edit : ")
x=df.loc[row_idx,col_idx]
ch=input("Do you really want to overwrite the value"+str(x)+" column(y/n)? ")
if ch=='y' or ch=='Y':
val=eval(input("Enter new value : "))
df.loc[row_idx,col_idx]=val
print("Value overwritten Successfully!!!")
print(df)
df.to_csv("literacy_rate.csv",index=False)
df=pd.read_csv("literacy_rate.csv")
else:
break

#***************************************************
# Calling main program
#***************************************************
MainMenu()

OUTPUT :
LITERACY.CSV - A CSV FILE
STATE/ UTS,1951,1961,1971,1981,1991,2001,2011
ANDAMAN AND NICOBAR ISLANDS,30.3,40.07,51.15,63.19,73.02,81.3,86.6
ANDHRA PRADESH,,21.19,24.57,35.66,44.08,60.47,67
ARUNACHAL PRADESH,,7.13,11.29,25.55,41.59,54.34,65.4
ASSAM,18.53,32.95,33.94,,52.89,63.25,72.2
BIHAR,13.49,21.95,23.17,32.32,37.49,47,61.8
CHANDIGARH,,,70.43,74.8,77.81,81.94,86
CHHATTISGARH,9.41,18.14,24.08,32.63,42.91,64.66,70.3
DADRA AND NAGAR HAVELI,,,18.13,32.9,40.71,57.63,76.2
DAMAN AND DIU,,,,,71.2,78.18,87.1
DELHI,,61.95,65.08,71.94,75.29,81.67,86.2
GOA,23.48,35.41,51.96,65.71,75.51,82.01,88.7
GUJARAT,21.82,31.47,36.95,44.92,61.29,69.14,78
HARYANA,,,25.71,37.13,55.85,67.91,75.6
HIMACHAL PRADESH,,,,,63.86,76.48,82.8
JAMMU AND KASHMIR,,12.95,21.71,30.64,,55.52,67.2
JHARKHAND,12.93,21.14,23.87,35.03,41.39,53.56,66.4
KARNATAKA,,29.8,36.83,46.21,56.04,66.64,75.4
KERALA,47.18,55.08,69.75,78.85,89.81,90.86,94
LAKSHADWEEP,15.23,27.15,51.76,68.42,81.78,86.66,91.8
MADHYA PRADESH,13.16,21.41,27.27,38.63,44.67,63.74,69.3
MAHARASHTRA,27.91,35.08,45.77,57.24,64.87,76.88,82.3
MANIPUR,12.57,36.04,38.47,49.66,59.89,70.53,76.9
MEGHALAYA,,26.92,29.49,42.05,49.1,62.56,74.4
MIZORAM,31.14,44.01,53.8,59.88,82.26,88.8,91.3
NAGALAND,10.52,21.95,33.78,50.28,61.65,66.59,79.6
ODISHA,15.8,21.66,26.18,33.62,49.09,63.08,72.9
PUDUCHERRY,,43.65,53.38,65.14,74.74,81.24,85.8
PUNJAB,,,34.12,43.37,58.51,69.65,75.8
RAJASTHAN,8.5,18.12,22.57,30.11,38.55,60.41,66.1
SIKKIM,,,17.74,34.05,56.94,68.81,81.4
TAMIL NADU,,36.39,45.4,54.39,62.66,73.45,80.1
TRIPURA,,20.24,30.98,50.1,60.44,73.19,87.2
UTTAR PRADESH,12.02,20.87,23.99,32.65,40.71,56.27,67.7
UTTARAKHAND,18.93,18.05,33.26,46.06,57.75,71.62,78.8
WEST BENGAL,24.61,34.46,38.86,48.65,57.7,68.64,76.3

You might also like