Final Coding
Final Coding
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import mysql.coonector as sqlcn
#Dataframe used
df=pd.read_csv
def line_chart1():
df=pd.read_csv(
df.sort_values(
df=df.loc[:,['Country','Total Medal']]
df1=df.head(10)
Countries=df1['Country']
Totalmedals=df1['Totalmedals']
plt.plot(Countries,Totalmedals,linestyle=':',color='green',marker='.')
plt.xlabel('Country~~~~~~>',fontsize=12,color='r')
plt.ylabel('Totalmedals~~~~>',fontsize=12,color='r')
plt.title("TOTAL MEDALS WON BY TOP 10 COUNTRIES\n",color='blue',fontsize=18)
plt.show()
def line_chart2():
df=pd.read_csv(
df.sort_values(
df=df.loc[:,['Country','SummerTimespart','Wintertimespart']]
df1=df.head(10)
Countries=df1['Country']
STotal=df1['Summertimespart']
WTotal=df1['Wintertimespart']
plt.plot(Countries,Totalmedals,linestyle=':',color='green',marker='.')
plt.xlabel('Country~~~~~~>',fontsize=12,color='r')
plt.ylabel('No of times participitated~~~~>',fontsize=12,color='r')
plt.title("TOTAL NUMBER OF TIMES PARTICIPITATED BY TOP 10 COUNTRIES\
n",color='blue',fontsize=18)
plt.legend()
plt.show()
def bar_chart1():
df=pd.read_csv(
df.sort_values(
df=df.loc[:,['Country','SummerTimespart','Wintertimespart']]
df1=df.head(n=10)
x=np.arange(len(df1))
Countries=df1['Country']
Totalgold=df1['Tgoldmedals']
plt.bar(x+0.25,totalgold,width=6,label='Total Number of gold medals by top 10
countries',color='gold')
plt.xticks(x,Countries,rotation=30)
plt.title("OLYMPICS GOLD MEDAL ANALYSIS BY TOP 10 COUNTRIES\
n",color='blue',fontsize=16)
plt.xlabel('Country~~~~~~>',fontsize=12,color='red')
plt.ylabel('No of Gold Medals~~~~>',fontsize=12,color='red')
plt.grid
plt.legend()
plt.show()
def bar_chart2():
df=pd.read_csv(
df.sort_values(
df=df.loc[:,['Country','SummerTimespart','Wintertimespart']]
df1=df.head(n=10)
x=np.arange(len(df1))
Countries=df1['Country']
Totalgold=df1['Tsilvermedals']
plt.bar(x+0.25,totalsilver,width=6,label='Total Number of silver medals by top 10
countries',color='silver')
plt.xticks(x,Countries,rotation=30)
plt.title("OLYMPICS SILVER MEDAL ANALYSIS BY TOP 10 COUNTRIES\
n",color='blue',fontsize=16)
plt.xlabel('Country~~~~~~>',fontsize=12,color='red')
plt.ylabel('No of Gold Medals~~~~>',fontsize=12,color='red')
plt.grid
plt.legend()
plt.show()
def bar_chart2():
df=pd.read_csv(
df.sort_values(
df=df.loc[:,['Country','SummerTimespart','Wintertimespart']]
df1=df.head(n=10)
x=np.arange(len(df1))
Countries=df1['Country']
Totalgold=df1['Tbronzemedals']
plt.bar(x+0.25,totalbronze,width=6,label='Total Number of bronze medals by top 10
countries',color='peru')
plt.xticks(x,Countries,rotation=30)
plt.title("OLYMPICS BRONZE MEDAL ANALYSIS BY TOP 10 COUNTRIES\
n",color='blue',fontsize=16)
plt.xlabel('Country~~~~~~>',fontsize=12,color='red')
plt.ylabel('No of Bronze Medals~~~~>',fontsize=12,color='red')
plt.grid
plt.legend()
plt.show()
def pie_chart():
df=pd.read_csv(
df.sum(axis=0,skipna=True)
lst=df.iloc[:,6:9],sum(axis=0)
lst.values.tolist()
clm=['Bronze','Silver','Gold']
plt.title("Medals distribution",color="navy")
plt.pie(lst,labels=clm,autopct='%1.1f%
%",color=['Bronze','Silver','Gold'],shadow=True)
plt.legend(loc='upperleft')
plt.show()
def dbargraph():
df=pd.read_csv(
df.sort_values(
df1=df.head(n=10)
x=np.arange(len(df1))
Countries=df1['Country']
Summermedal=df1['SummerMedal']
Wintermedal=df1['WinterMedal']
plt.bar(x-0.2,Summermedal,width=0.4,label='Total Number of medals by top 10
countries in Summer',color='orangered')
plt.bar(x+0.2,Wintermedal,width=0.4,label='Total Number of medals by top 10
countries in Winter',color='grey')
plt.xticks(x,Countries,rotation=20)
plt.title("OLYMPICS MEDAL ANALYSIS BY TOP 10 COUNTRIES\
n",color='navy',fontsize=16)
plt.xlabel('Country~~~~~~>',fontsize=12,color='r')
plt.ylabel('No of Medals~~~~>',fontsize=12,color='r')
plt.grid
plt.legend()
plt.show()
def odanalysis():
while True:
print("<.................>")
print("Data Frame Analysis")
print("<.................>")
mn='''1)To print top records by Total medals won
\n 2) To print top records by Total gold medals won
\n 3) To print top records by Total silver medals won
\n 4) To print top records by Total bronze medals won
\n 5) To print Bottom records
\n 6) To print the general information of the dataframe
\n 7) To describe the dataframe
\n 8) To print specified column data
\n 9) To print maximum value for each column
\n 10)To go back to main menu
print(mn)
x=int(input("Enter your choice"))
print("----------x-----------------------x------------------------x-------------
x")
df=pd.read_csv(
if x==1:
df=df.sort_values('TotalMedal',ascending = False)
nor=int(input("Enter the number of records to be displayed "))
print("Top",nor,"Records from Dataframe")
print(df.head(nor))
print("----------x-----------------------x------------------------
x-------------x")
if x==2:
df=df.sort_values('Tgoldmedal',ascending = False)
nor=int(input("Enter the number of records to be displayed "))
print("Top",nor,"Records by total number of gold medals")
print(df.head(nor))
print("----------x-----------------------x------------------------
x-------------x")
if x==3:
df=df.sort_values('Tsilvermedal',ascending = False)
nor=int(input("Enter the number of records to be displayed "))
print("Top",nor,"Records by total number of silver medals")
print(df.head(nor))
print("----------x-----------------------x------------------------
x-------------x")
if x==4:
df=df.sort_values('Tbronzemedal',ascending = False)
nor=int(input("Enter the number of records to be displayed "))
print("Top",nor,"Records by total number of bronze medals")
print(df.head(nor))
print("----------x-----------------------x------------------------
x-------------x")
if x==5:
df=df.sort_values('TotalMedal',ascending = False)
nor=int(input("Enter the number of records to be displayed "))
print("Bottom",nor,"Records from Dataframe")
print(df.tail(nor))
print("----------x-----------------------x------------------------
x-------------x")
if x==6:
print("Information of Dataframe")
print(df.info())
print("----------x-----------------------x------------------------
x-------------x")
if x==7:
print("Describing the basic characteristics of Dataframe")
print(df.describe())
print("----------x-----------------------x------------------------
x-------------x")
if x==8:
print("Name of the columns~~>",df.columns)
clm=eval(input("Enter the columns name in a list"))
print(df[clm])
print("----------x-----------------------x------------------------
x-------------x")
if x==9:
print("Maximum calue for each column")
print(df.apply(np.max))
print("----------x-----------------------x------------------------
x-------------x")
if x==10:
menu()
break
def read_csv_excel():
ans=True
while ans:
print('''1)Read CSV file and display dataframe
\n2) Read excel file and display dataframe
\n3) Press 3 to go back to main menu
ans=int(input("Enter your choice"))
if ans==1:
df=pd.read_csv(
print(df)
print("Done!")
elif ans==2:
fname=input("Enter filename with PATH and EXTENSIONS(.xls/.xlsx):")
df=pd.read_excel(fname)
print(df)
print("done!")
elif ans==3:
menu()
# Importing and exporting data
def imp_exp_sql():
while True:
print("\n\n"+"*"*60)
print("Data transfer to/from MYSQL")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print('''1-Import from MYSQL to creat and display dataframe\
\n\n2-Export from Dataframe to MYSQL\
\n\n3-Press to go back''')
ans=int(input("Enter your choice"))
if ans==1:
sql2df()
if ans==1:
df2sql()
if ans==3:
menu()
else:
print("Invalid input.Try again")
def sql2df():
db_conn=sqlcn.connect(host='localhost',database='obd',
user='root',password='1244')
fname=input("Enter the tablename:")
db_cursor=db_conn.cursor()
qry='Select*from' +fname+""
db_cursor.execute(qry)
table_rows=db_cursor.fetchall()
for x in tablerows:
print(x)
db_cursor.close()
db_conn.close()
print("....Done!")
def df2sql():
df=pd.read_csv(
df1=df.head(n=11)
print(df1)
db_conn=sqlcn.connect(host='localhost',user='root'
password='1244',database='odb')
tblname=input("Enter the table name:")
db_cursor = db_conn.cursor()
db_cursor.execute("Show Tables Like" +tblname+" ' ")
if db_cursor.fetchone():
db_cursor2=db.conn.cursor()
db_cursor2.execute("Delete from"+tblname)
db_conn.commit()
for rows,rs in df1.itterrows():
Country=rs[0]
SummerTimesPart=str(rs[1])
SummerTotal=str(rs[2])
WinterTimesPart=str(rs[3])
WinterTOtal=str(rs[4])
TotalTimesPart=str(rs[5])
TBronzeMedal=str(rs[6])
TSilverMedal=str(rs[7])
TGoldMedal=str(rs[8])
TotalMedal=str(rs[9])
qry2="Insert into"+tblname+"values('"+Country+"',"+SummerTimesPart+",\
"+SummerTotal+","+WinterTimesPart+","+WinterTotal+","+TotalTimesPart+",\
"+TBronzeMedal+","+TSilverMedal+","+TGoldMedal+","+TotalMedal+")"
db_cursor2.execute(qry2)
db_conn.commit()
db_cursor2.close()
print("Data transfer from dataframe to mysql>>SUCESSFULL...!")
else:
qry3="Create Table"+tblname+"(Country varchar(20),SummerTimesPart
int,SummerTotal int,\
WinterTimesPart int,WinterTotal int,TotalTimesPart int,TBronzeMedal int,
TSilverMedal int,\
TGoldMedal int,TotalMedal int)"
db_cursor2=db.conn.cursor()
db_cursor2.execute(qry3)
db_conn.commit()
for rows,rs in df1.itterrows():
Country=rs[0]
SummerTimesPart=str(rs[1])
SummerTotal=str(rs[2])
WinterTimesPart=str(rs[3])
WinterTOtal=str(rs[4])
TotalTimesPart=str(rs[5])
TBronzeMedal=str(rs[6])
TSilverMedal=str(rs[7])
TGoldMedal=str(rs[8])
TotalMedal=str(rs[9])
qry2="Insert into"+tblname+"values('"+Country+"',"+SummerTimesPart+",\
"+SummerTotal+","+WinterTimesPart+","+WinterTotal+","+TotalTimesPart+",\
"+TBronzeMedal+","+TSilverMedal+","+TGoldMedal+","+TotalMedal+")"
db_cursor2.execute(qry2)
db_conn.commit()
db_conn.close()
print("Data transfer from dataframe to mysql>>SUCESSFULL...!")
#Manipulation of data
def manuplt():
df=pd.read_csv(
ans=True
while ans:
print('''Data Manipulation\n
1) Inserting a row
2) Deleting a row
3) Inserting a column
4) Deleting a column
5) Renaming a column
6) Exit to main menu''')
ans=int(input("Enter Your choice"))
if ans==1:
col=df.columns
lst=eval(input("Enter the row values in list:"))
sr=pd.Series(lst,index=col)
row_df1=pd.DataFrame([sr])
df=pd.concat([row_df1,df],ignore_index=True)
print(df)
print("Row added Successfully")
print('~'*30)
elif ans==2:
inp=int(input("Enter the row's index you want to be deleted:")
df1=df.drop(inp,axis=0)
print("~"*30)
print("Dataframe after row index no.",inp,"is deleted..")
print("~"*30)
print(df1)
print("~"*30)
elif ans==3:
pd.set_option('display.width',500)
pd.set_option('display.max_columns',None)
clname=input('Enter the column name")
inp=int(input("Enter the column index no.\n Where you want to input the
column:'))
df.insert(inp,clname,"Nan")
print(df)
print("~"*30)
elif ans==4:
pd.set_option('display.width',500)
pd.set_option('display.max_columns',None)
print("Dataframe before deleting the column")
print(df)
inp=input("Column name you want to delete")
df=df.drop(inp,axis=1)
print("Dataframe after deleting the column",inp":")
print(df)
print("~"*30)
elif ans==5:
pd.set_option('display.width',500)
pd.set_option('display.max_columns',None)
print("~"*30)
print("Dataframe before deleting the column name/s")
print("~"*30)
print(df)
oldcm=input("Enter the column name you want to rename")
newcm=input("Enter the new column name")
df=df.rename(columns={oldcm:newcm})
print("~"*30)
print("Dataframe after changing the columns name/s")
print("~"*30)
print(df)
print("~"*30)
elif ans==6:
print("Returning to main menu..Done")
menu()
menu()