Text
Text
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')
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')
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']
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")
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)
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()
#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:")
#Main Menu