Question

In: Computer Science

build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into...

build a python program that will be performing:

- Read a CSV file 'annual.csv' enterprise into a data structure

- Count the number of rows and columns

- Determine if the data contains empty values

- Replace the empty values by 'NA' for strings, '0' for decimals and '0.0' for floats

- Transform all Upper case characters to Lower case characters

- Transform all Lower case characters to Upper case characters

- save back the 'repaired' array as csv

- Print out the size of the data (number of rows, number of columns)

Difficulty:

-- be sure that each row has the same length (number of elements)

- the length of each row should be the same as the header.

Solutions

Expert Solution

The below code done using Pandas and numpy library in Python:

importing pandas and numpy:

import pandas as pd
import numpy as np


- Reading CSV file:

 annual = pd.read_csv(r"annual.csv")

- Counting the number of rows and columns

print(annual.shape())

-Determining if the data contains empty values:

Prints number of column wise null values :

print(annual.isnull().sum())

- Replacing the empty values by 'NA' for strings, '0' for decimals and '0.0' for floats

a1 = annual.select_dtypes(include = 'int64')
a1.fillna("0", inplace = True) 

a2 = annual.select_dtypes(include = 'float64')
a2.fillna("0.0",inplace=True)

a3 = annual.select_dtypes(include = 'object')
a3.fillna("na",inplace=True)

- Transform all Upper case characters to Lower case character

annual.str.lower()

-Transform all Lower case characters to Upper case characters

annual.str.upper()

-save back the 'repaired' array as csv

repaired.to_csv('repaired.csv')

-Print out the size of the data (number of rows, number of columns):

print(annual.shape())

be sure that each row has the same length (number of elements) the length of each row should be the same as the header :

for this we have remove rows with null values:

annual=annual.dropna()

Related Solutions

Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Step by step in python please Write a program this will read a file (prompt for...
Step by step in python please Write a program this will read a file (prompt for name) containing a series of numbers (one number per line), where each number represents the radii of different circles. Have your program output a file (prompt for name) containing a table listing: the number of the circle (the order in the file) the radius of the circle the circumference the area of the circle the diameter of the circle Use different functions to calculate...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Language: Python Function name : sort_by_rating Parameters : file (.csv file), category (string), order (boolean) Returns:...
Language: Python Function name : sort_by_rating Parameters : file (.csv file), category (string), order (boolean) Returns: None Description : You want to see what order the items of a particular category in your file would be in if you sorted them by rating. Given a file, a category of clothing, and an order boolean (True for ascending order, False for descending order), create a function that writes a file called “sorted_items.csv” that includes all of the items in the specified...
{PYTHON }You have a CSV file containing the location and population of various cities around the...
{PYTHON }You have a CSV file containing the location and population of various cities around the world. For this question you'll be given a list of cities and return the total population across all those cities. Write a function named "total_population" that takes a string then a list as parameters where the string represents the name of a CSV file containing city data in the format "CountryCode,CityName,Region,Population,Latitude,Longitude" and the second parameter is a list where each element is itself a...
{PYTHON }You have a CSV file containing the location and population of various cities around the...
{PYTHON }You have a CSV file containing the location and population of various cities around the world. For this question you'll be given a list of cities and return the total population across all those cities. Write a function named "total_population" that takes a string then a list as parameters where the string represents the name of a CSV file containing city data in the format "CountryCode,CityName,Region,Population,Latitude,Longitude" and the second parameter is a list where each element is itself a...
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 python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
How to read the given structure from a random CSV file separated by commas(which contains no...
How to read the given structure from a random CSV file separated by commas(which contains no headers only the values of the contents of the structure) and then insert in a binary search tree using one of the structure contents as a key i.e. datetime and handle duplicates in binary search tree by implementing link_list.Please develop a C code for this. struct data{ char biker_id[200]; char distance_bike_travelled[200]; char datetime[200]; char count_tripr[200]; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT