Question

In: Computer Science

Write a program that reads and parses the CSV file, and can sort and filter data...

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

Solutions

Expert Solution

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*


Related Solutions

Create a program that parses a CSV file of product data and prints the items with...
Create a program that parses a CSV file of product data and prints the items with a price that is less than or equal to that input by the user. • Your program should take two arguments: an input file to process and a price limit. • Print only the names of each item to stdout that have a price less than or equal to the given limit. • If the given file does not exist or cannot be opened,...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv),...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv), which contains passenger counts for February 2019 on 200 international flights. The data set (attached below) is a modified CSV file on all International flight departing from US Airports between January and June 2019 reported by the US Department of Transportation. You write a program that give some summary statistics on this data set. Create a header file named flights.h. In this file, you...
Write a program that reads in a continuous stream of strings representing a line of CSV...
Write a program that reads in a continuous stream of strings representing a line of CSV data in the format "NAME,AGE,EMAIL,DOB". Implement a function check_csv which verifies that each input string matches this format by ensuring that: • There are 4 tokens in the string corresponding to the 4 properties above. • The second token MUST be a number. • The fourth token should be in the format MM-DD-YYYY (hint: tokenize this as well). The function should return 0 if...
I am trying to create a program that reads from a csv file and finds the...
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:     for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):         index +=1         print("{}. {} {:.2f}".format(county, sums_by_volume[county]))      When I run...
Write a C++ or program that parses (and stores into a variable of the correct type)...
Write a C++ or program that parses (and stores into a variable of the correct type) a string into 4 fields separated by a colon (:): Field 1: A non-empty string value that does not contain a colon Filed 2: An odd integer, or empty Filed 3: One of these values: red, orange, yellow, green, blue, indigo, violet Field 4: Another string value (may contain colon characters) output: Accept: sample:556989:green:Longer example string :) Reject (bad color): sample:556989:purple:Longer example string :)...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
Write a Java program that reads an input graph data from a user. Then, it should...
Write a Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number of vertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2 0...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into a vector and loop through that vector and find the max number.
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT