In: Computer Science
Write a program that reads and parses the CSV file, and can sort and filter data according to the user input and print the results.
Your program should be able to do the following according to the user input:
1. Show results from a specific country
2. Sort the data according to any column
3. Filter the results (for example, >10000 cases)
4. Print the top or bottom n results
You get bonus points if your program
• Can do partial country name search, meaning ”Tur” matches Turkey, ”Aus” matches Austria and Australia, etc.
• Saves the state of the CSV file (ordering) for later launches of the application.
• Visualizes the data in some way.
NOTE: While we cannot attach any file to a Chegg question, you can create an empty csv (Excel) file and paste the parameters below, into this file please. (There are more rows in the original file).
Country/Region,Confirmed,Deaths,Recovered,Active,New cases,New deaths,New recovered,Deaths / 100 Cases,Recovered / 100 Cases,Deaths / 100 Recovered,Confirmed last week,1 week change,1 week % increase,WHO Region |
Afghanistan,32672,826,19164,12682,348,7,1833,2.53,58.66,4.31,30616,2056,6.72,Eastern Mediterranean |
Albania,2819,74,1637,1108,67,2,45,2.63,58.07,4.52,2330,489,20.99,Europe |
Cameroon,12592,313,10100,2179,0,0,0,2.49,80.21,3.1,12592,0,0.0,Africa |
Canada,107172,8731,0,98441,223,10,0,8.15,0.0,Infinity,104865,2307,2.2,Americas |
Turkey,204610,5206,179492,19912,1154,20,1214,2.54,87.72,2.9,195883,8727,4.46,Europe |
US,2839436,129676,894325,1815435,45283,242,103921,4.57,31.5,14.5,2510259,329177,13.11,Americas |
Uganda,927,0,868,59,16,0,19,0.0,93.64,0.0,848,79,9.32,Africa |
Ukraine,48628,1243,21907,25478,923,16,611,2.56,45.05,5.67,42932,5696,13.27,Europe |
import pandas as pd
if name == '__main__':
try:
dataset = pd.read_csv("dataset.csv")
while(True):
print("1.Show results from a specific country","2.Sort the data
according to any column","3.Filter the results"
,"4.Print top n resuls","5.Print bottom n results","6.Save
dataset","7.Exit",sep='\n')
choice = input()
if choice == '1':
print("Enter full/partial country name")
country = input()
print(dataset.loc[dataset["Country/Region"].str.contains(country)].stack(dropna=False))
elif choice == '2':
column_list = (dataset.columns.values)
for column_name in column_list:
print(column_name)
print("Select column name to sort")
column_name = input()
if column_name in column_list:
print("1.Ascending\n2.Descending")
sort_order = input()
if sort_order == '1':
dataset.sort_values(by=[column_name],inplace=True)
elif sort_order == '2':
dataset.sort_values(by=[column_name],inplace=False,ascending=False)
else:
print("Select valid column name")
elif choice == '3':
column_list = (dataset.columns.values)
for column_name in column_list:
print(column_name)
print("Select column name to filter")
filter_column = input()
if filter_column in column_list:
print("Enter value to filter")
filter_value = input()
if dataset[filter_column].dtypes == 'int64':
filter_value = int(filter_value)
elif dataset[filter_column].dtypes == 'float64':
filter_value = float(filter_value)
print('Filter options\n1.Greater than\n2.Greater than equal
to\n3.Less than\n4.Less than equal to\n',
'5.Equal\n6.Not equal to')
filter_choice = input()
if filter_choice == '1':
print(dataset[dataset[filter_column] > filter_value])
elif filter_choice == '2':
print(dataset[dataset[filter_column] >= filter_value])
elif filter_choice == '3':
print(dataset[dataset[filter_column] < filter_value])
elif filter_choice == '4':
print(dataset[dataset[filter_column] <= filter_value])
elif filter_choice == '5':
print(dataset[dataset[filter_column] == filter_value])
elif filter_choice == '6':
print(dataset[dataset[filter_column] != filter_value])
else:
print("Enter valid choice")
else:
print('Enter valid column name')
elif choice == '4':
print("Enter number of rows")
try:
num = int(input())
print(dataset.head(num))
except:
print("Enter valid number")
elif choice == '5':
print("Enter number of rows")
try:
num = int(input())
print(dataset.tail(num))
except:
print("Enter valid number")
elif choice == '6':
print("Enter filename")
filename = input()
try:
dataset.to_csv(filename+".csv",index=False)
except:
print("Failed to save file")
elif choice == '7':
break
else:
print("Wrong choice")
except:
print("Failed to open CSV file")
*Change csv name as your file name*