A csv stands for "comma separated values", which is defined as a simple file format that uses specific structuring to arrange tabular data. It stores tabular data such as spreadsheet or database in plain text and has a common format for data interchange. A csv file opens into the excel sheet, and the rows and columns data define the standard format.
The CSV module work is used to handle the CSV files to read/write and get data from specified columns. There are different types of CSV functions, which are as follows:
In Python, csv.reader() module is used to read the csv file. It takes each row of the file and makes a list of all the columns.
We have taken a txt file named as python.txt that have default delimiter comma(,) with the following data:
name,department,birthday month Parker,Accounting,November Smith,IT,October
import csv with open('python.txt') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') line_count = 0 for row in csv_reader: if line_count == 0: print(f'Column names are {", ".join(row)}') line_count += 1
We can also use DictReader to read the csv file directly into a dictionary rather than deal with a list of individual String elements.
Again, our input file, python.txt is as follows:
name,department,birthday month Parker,Accounting,November Smith,IT,October
import csv with open('python.txt', mode='r') as csv_file: csv_reader = csv.DictReader(csv_file) line_count = 0 for row in csv_reader: if line_count == 0: print(f'The Column names are as follows {", ".join(row)}') line_count += 1 print(f'\t{row["name"]} works in the {row["department"]} department, and was born in {row["birthday month"]}.') line_count += 1 print(f'Processed {line_count} lines.')
Pandas is defined as an open-source library which is built on the top of the NumPy library. It provides fast analysis, data cleaning, and preparation of the data for the user.
Reading the csv file into a pandas DataFrame is quick and straight forward. We don't need to write enough lines of code to open, analyze, and read the csv file in pandas and it stores the data in DataFrame.
Here, we are taking a slightly more complicated file to read, called hrdata.csv, which contains data of company employees.
Name,Hire Date,Salary,Leaves Remaining John Idle,08/15/14,50000.00,10 Smith Gilliam,04/07/15,65000.00,8 Parker Chapman,02/21/14,45000.00,10 Jones Palin,10/14/13,70000.00,3 Terry Gilliam,07/22/14,48000.00,7 Michael Palin,06/28/13,66000.00,8
import pandas df = pandas.read_csv('hrdata.csv') print(df)
In the above code, the three lines are enough to read the file, and only one of them is doing the actual work, i.e., pandas.read_csv()