In: Computer Science
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.
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()