You are on page 1of 7

mport matplotlib.

pyplot as plt
import pandas as pd
import numpy as np

def main_menu():
print("**************************")
print("Read Data From File in Different Way")
print("1. Read complete csv file")
print("2. Reading complete file without index")
print("===============")
print("Data Visualization")
print("3. Line Chart")
print("4. Bar Plot")
print("5. Pie chart")
print("6. Scatter chart")
print("===============")
print("Apply Data Manipulation in the records of CSV file")
print("7. Sorting the data as per your choice")
print("8. Read Top and Bottom Records from file as per requirment")
print("9. Make the copy of CSV file")
print("10. Read the Specific columns")
print("**************************")
print("11. Add the New State Record in File")
print("12. Search the Records as per State Name")
print("**************************")
print("13. Statistical Function on Records")
print("**************************")

main_menu()

def ReadCSV():
print("Reading Data from CSV file")
df=pd.read_csv("states.csv")
print(df)

def no_index():
print("Reading Data from CSV file without index value")
df=pd.read_csv("states.csv",index_col=0)
print(df)

def new_column_name():
print("Adding new column name to existing Data")
df=pd.read_csv("states.csv",index_col=0,skiprows=1,names=['State Name','Total
Cases','Recovered Cases','Total Death','Active Cases'])
print(df)

def line_plot():
df=pd.read_csv("states.csv")
st=df['State']
cnf=df['Confirmed']
rc=df['Recovered']
dth=df['Deaths']
ac=df['Active']
plt.xlabel("state")
plt.xticks(rotation='vertical')

print("Select Specific Line Chart as given below:")


print("press 1 to print the data for State vs Confirmed Cases")
print("press 2 to print the data for State vs Recovered Cases")
print("press 3 to print the data for State vs Death Cases")
print("press 4 to print the data for State vs Active Cases")
print("press 5 to merge all the data in one Line chart")

op=int(input("Enter your choice:"))

if op==1:
plt.ylabel("Confirmed cases")
plt.title("State wise Confirmed Cases")
plt.plot(st,cnf)
plt.show()
elif op==2:
plt.ylabel("Recovered cases")
plt.title("State wise Recovered Cases")
plt.plot(st,rc)
plt.show()
elif op==3:
plt.ylabel("Death cases")
plt.title("State wise Death Cases")
plt.plot(st,dth)
plt.show()
elif op==4:
plt.ylabel("Active cases")
plt.title("State wise Active Cases")
plt.plot(st,ac)
plt.show()
elif op==5:
plt.ylabel("Number of cases")
plt.plot(st,cnf,label="State wise Confirmed Cases")
plt.plot(st,rc,label="State wise Recovered Cases")
plt.plot(st,dth,label="State wise Death")
plt.plot(st,ac,label="State wise Active Cases")
plt.legend()
plt.show()
else:
print("Enter valid input")

def bar_plot():
df=pd.read_csv("states.csv")
st=df['State']
cnf=df['Confirmed']
rc=df['Recovered']
dth=df['Deaths']
ac=df['Active']
plt.xlabel("state")
plt.xticks(rotation='vertical')

print("Select Specific Bar Chart as given below:")


print("press 1 to print the data for State vs Confirmed Cases")
print("press 2 to print the data for State vs Recovered Cases")
print("press 3 to print the data for State vs Death")
print("press 4 to print the data for State vs Active Cases")
print("press 5 to print all the data in form of stack bar chart")
print("press 6 to print all the data in form of multi bar chart")

op=int(input("Enter your choice:"))

if op==1:
plt.ylabel("Confirmed cases")
plt.title("State wise Confirmed Cases")
plt.bar(st,cnf)
plt.show()
elif op==2:
plt.ylabel("Recovered cases")
plt.title("State wise Recovered Cases")
plt.bar(st,rc)
plt.show()
elif op==3:
plt.ylabel("Death cases")
plt.title("State wise Death Cases")
plt.bar(st,dth)
plt.show()
elif op==4:
plt.ylabel("Active cases")
plt.title("State wise Active Cases")
plt.bar(st,ac)
plt.show()
elif op==5:
plt.bar(st,cnf,width=0.2,label="State wise Confirmed Cases")
plt.bar(st,rc,width=0.2,label="State wise Recovered Cases")
plt.bar(st,dth,width=0.2,label="State wise Death")
plt.bar(st,ac,width=0.2,label="State wise Active Cases")
plt.legend()
plt.show()
elif op==6:
ind=np.arange(len(st))
width=0.25
plt.bar(ind,cnf,width,label="State wise Confirmed Cases")
plt.bar(ind+0.25,rc,width,label="State wise Recovered Cases")
plt.bar(ind+0.50,dth,width,label="State wise Death")
plt.bar(ind+0.75,ac,width,label="State wise Active Cases")
plt.legend()
plt.show()
else:
print("Enter valid input")

def pie_plot():
df=pd.read_csv("states.csv")
st=df['State']
cnf=df['Confirmed']
rc=df['Recovered']
dth=df['Deaths']
ac=df['Active']

print("Select Specific Pie Chart as given below:")


print("press 1 to print the data for State vs Confirmed Cases")
print("press 2 to print the data for State vs Recovered Cases")
print("press 3 to print the data for State vs Death")
print("press 4 to print the data for State vs Active Cases")

op=int(input("Enter your choice:"))

if op==1:
plt.title("State wise Confirmed Cases")
plt.pie(cnf,labels=st,autopct="%3d%%")
#autopct attribute can be used to show the percentage of the data.
plt.show()
elif op==2:
plt.pie(rc,labels=st,autopct="%3d%%")
plt.title("State wise Recovered Cases")
plt.show()

elif op==3:
plt.pie(dth,labels=st,autopct="%3d%%")
plt.title("State wise Death Cases")
plt.show()

elif op==4:
plt.pie(ac,labels=st,autopct="%3d%%")
plt.title("State wise Active Cases")
plt.show()

def scatter_chart():
df=pd.read_csv("states.csv")
st=df['State']
cnf=df['Confirmed']
rc=df['Recovered']
dth=df['Deaths']
ac=df['Active']

ax=plt.gca()
ax.scatter(st,cnf,color='b',label="State wise Confirmed Cases")
ax.scatter(st,rc,color='r',label="State wise Recovered Cases")
ax.scatter(st,dth,color='g',label="State wise Death")
ax.scatter(st,ac,color='y',label="State wise Active Cases")

plt.xlabel("state")
plt.xticks(rotation='vertical')
plt.title("Complete Scatter chart")
plt.legend()
plt.show()

def data_sorting():
df=pd.read_csv("states.csv")

print("Press 1 to arrange the record as per the State Name")


print("Press 2 to arrange the record as per the Confirmed Cases")
print("Press 3 to arrange the record as per the Recovered Cases")
print("Press 4 to arrange the record as per the Total Deaths")
print("Press 5 to arrange the record as per the Active Cases")

op=int(input("Enter Your choice:"))


if op==1:
df.sort_values(["State"],inplace=True)
#inplace: make the changes in passed DataFrame
print(df)
elif op==2:
df.sort_values(["Confirmed"],inplace=True)
print(df)
elif op==3:
df.sort_values(["Recovered"],inplace=True)
print(df)
elif op==4:
df.sort_values(["Deaths"],inplace=True)
print(df)
elif op==5:
df.sort_values(["Active"],inplace=True)
print(df)
else:
print("Enter valid input")

def top_bottom_selected_records():
df=pd.read_csv("states.csv",index_col=0)
top=int(input("How many records to display from top:"))
print("First",top,"records")
print(df.head(top))
bottom=int(input("How many records to display from bottom:"))
print("Last",bottom,"records")
print(df.tail(bottom))

def duplicate():
print("Duplicate the file with new file")
df=pd.read_csv("states.csv")
df.to_csv("D:\statesNew.csv")
print("Data from the new file")
print(df)

def specific_col():
print("Reading Specific Column from CSV file")
df=pd.read_csv("states.csv",usecols=['State','Recovered'],index_col=0)
print(df)

#to add new reocrd in the csv file


def AddNewState():
df=pd.read_csv("states.csv")
df['Confirmed']=df['Confirmed'].astype('int')
df['Recovered']=df['Recovered'].astype('int')
df['Deaths']=df['Deaths'].astype('int')
df['Active']=df['Active'].astype('int')
print(df)

#Enter all the details which present in csv file

sname=input("State Name=")
C_Case=int(input("Enter Confirmed cases="))
R_Case=int(input("Enter Recovered cases="))
D_Case=int(input("Enter Deaths cases="))
A_Case=int(input("Enter Active cases="))
#To get total number of records present in csv file, will store in n
n=df['State'].count()

#To add all the details


df.at[n]=[sname,C_Case,R_Case,D_Case,A_Case]
df.to_csv("states.csv",index=False)
df['Confirmed']=df['Confirmed'].astype('int')
df['Recovered']=df['Recovered'].astype('int')
df['Deaths']=df['Deaths'].astype('int')
df['Active']=df['Active'].astype('int')
print(df)

#To search any record as per State Name


def SearchState():
df=pd.read_csv("states.csv")
df['Confirmed']=df['Confirmed'].astype('int')
df['Recovered']=df['Recovered'].astype('int')
df['Deaths']=df['Deaths'].astype('int')
df['Active']=df['Active'].astype('int')

#print(df)
sname=input("Enter State Name:")
#To store state name in variable s
s=df.loc[df["State"]==sname]

if s.empty:
print("No Record Found With the State name:",sname)
else:
print("Details with State Name:",sname)
print(s)

#statistical functions
def statistical_function():
print("press 1 to find the minimum value for Confirmed Cases:")
print("press 2 to find the maximum value for Recovered Cases:")
print("press 3 to find the Total Death Cases:")
print("press 4 to find the average value for Active Cases:")
print("press 5 to find the Total Number of State in CSV File:")

ch=int(input("Enter your choice:"))


if ch==1:
df=pd.read_csv("states.csv")
print(df["Confirmed"].min())
elif ch==2:
df=pd.read_csv("states.csv")
print(df["Recovered"].max())
elif ch==3:
df=pd.read_csv("states.csv")
print(df["Deaths"].sum())
elif ch==4:
df=pd.read_csv("states.csv")
print(df["Active"].mean())
elif ch==5:
df=pd.read_csv("states.csv")
print(df["State"].count())
elif ch==6:
df=pd.read_csv("Pokemon.csv")
print(df["Speed"].min())
elif ch==7:
df=pd.read_csv("Pokemon.csv")
print(df["Speed"].mean())
else:
print("Enter valid input")

#Main Menu

opt=int(input("enter your choice="))


if opt==1:
ReadCSV()
elif opt==2:
no_index()
elif opt==3:
line_plot()
elif opt==4:
bar_plot()
elif opt==5:
pie_plot()
elif opt==6:
scatter_chart()
elif opt==7:
data_sorting()
elif opt==8:
top_bottom_selected_records()
elif opt==9:
duplicate()
elif opt==10:
specific_col()
elif opt==11:
AddNewState()
elif opt==12:
SearchState()
elif opt==13:
statistical_function()
else:
print("enter valid input")

You might also like