Javascript required
Skip to content Skip to sidebar Skip to footer

Reading a Column From Csv File and Make Call to Api

Intro: In this commodity, I will walk y'all through the different ways of reading and writing CSV files in Python.

Table of Contents:

  1. What is a CSV?
  2. Reading a CSV
  3. Writing to a CSV

1. What is a CSV?

CSV stands for "Comma Separated Values." It is the simplest course of storing information in tabular course as evidently text. Information technology is important to know to work with CSV because nosotros by and large rely on CSV data in our day-to-solar day lives as data scientists.

Structure of CSV:

We have a file named "Salary_Data.csv." The first line of a CSV file is the header and contains the names of the fields/features.

Later on the header, each line of the file is an observation/a record. The values of a tape are separated by "comma."

2. Reading a CSV

CSV files can be handled in multiple ways in Python.

two.i Using csv.reader

Reading a CSV using Python's inbuilt module called csv using csv.reader object.

Steps to read a CSV file:

1. Import the csv library

import csv

ii. Open up the CSV file

The .open() method in python is used to open files and return a file object.

file = open('Salary_Data.csv')  type(file)

The type of file is "_io.TextIOWrapper" which is a file object that is returned by the open() method.

3. Use the csv.reader object to read the CSV file

csvreader = csv.reader(file)

four. Extract the field names

Create an empty listing called header. Utilise the next() method to obtain the header.

The .next() method returns the current row and moves to the next row.

The first fourth dimension you run adjacent() information technology returns the header and the next fourth dimension y'all run it returns the first record and and so on.

header = [] header = next(csvreader) header

5. Extract the rows/records

Create an empty listing called rows and iterate through the csvreader object and append each row to the rows list.

rows = [] for row in csvreader:         rows.append(row) rows

6. Close the file

.close() method is used to shut the opened file. In one case it is closed, nosotros cannot perform whatever operations on it.

file.close()

Consummate Code:

import csv file = open("Salary_Data.csv") csvreader = csv.reader(file) header = next(csvreader) print(header) rows = [] for row in csvreader:     rows.append(row) print(rows) file.close()

Naturally, we might forget to close an open file. To avoid that we can use the with()argument to automatically release the resources. In simple terms, there is no need to call the .close() method if we are using with() argument.

Implementing the above code using with() statement:

Syntax: with open up(filename, mode) equally alias_filename:

Modes:

'r' – to read an existing file,
'due west' – to create a new file if the given file doesn't exist and write to it,
'a' – to append to existing file content,
'+' –  to create a new file for reading and writing

import csv rows = [] with open("Salary_Data.csv", 'r) as file:     csvreader = csv.reader(file)     header = side by side(csvreader)     for row in csvreader:         rows.append(row) impress(header) print(rows)

2.2 Using .readlines()

Now the question is – "Is it possible to fetch the header, rows using only open() and with() statements and without the csv library?" Permit's see…

.readlines() method is the answer. It returns all the lines in a file as a list. Each particular of the listing is a row of our CSV file.

The first row of the file.readlines() is the header and the rest of them are the records.

with open('Salary_Data.csv') as file:     content = file.readlines() header = content[:i] rows = content[1:] print(header) print(rows)

**The 'n' from the output tin be removed using .strip() method.

What if nosotros have a huge dataset with hundreds of features and thousands of records. Would it exist possible to handle lists??

Hither comes the pandas library into the picture.

2.iii Using pandas

Steps of reading CSV files using pandas

1. Import pandas library

import pandas equally pd

2. Load CSV files to pandas using read_csv()

Basic Syntax: pandas.read_csv(filename, delimiter=',')

data= pd.read_csv("Salary_Data.csv") data

3. Excerpt the field names

.columns is used to obtain the header/field names.

data.columns

four. Excerpt the rows

All the data of a information frame can be accessed using the field names.

information.Salary

3. Writing to a CSV file

Nosotros can write to a CSV file in multiple ways.

3.1 Using csv.writer

Allow's assume we are recording iii Students data(Name, M1 Score, M2 Score)

header = ['Name', 'M1 Score', 'M2 Score'] information = [['Alex', 62, lxxx], ['Brad', 45, 56], ['Joey', 85, 98]]

Steps of writing to a CSV file:

1. Import csv library

import csv

2. Define a filename and Open the file using open up()

three. Create a csvwriter object using csv.writer()

four. Write the header

v. Write the rest of the data

code for steps 2-5

filename = 'Students_Data.csv' with open(filename, 'w', newline="") every bit file:     csvwriter = csv.author(file) # 2. create a csvwriter object     csvwriter.writerow(header) # 4. write the header     csvwriter.writerows(data) # five. write the residual of the data

Below is how our CSV file looks.

3.2 Using .writelines()

Iterate through each list and convert the listing elements to a string and write to the csv file.

header = ['Name', 'M1 Score', 'M2 Score'] information = [['Alex', 62, fourscore], ['Brad', 45, 56], ['Joey', 85, 98]] filename = 'Student_scores.csv' with open(filename, 'due west') as file:     for header in header:         file.write(str(header)+', ')     file.write('n')     for row in data:         for 10 in row:             file.write(str(10)+', ')         file.write('n')

three.three. Using pandas

Steps to writing to a CSV using pandas

one. Import pandas library

import pandas as pd

ii. Create a pandas dataframe using pd.DataFrame

Syntax: pd.DataFrame(information, columns)

The data parameter takes the records/observations and the columns parameter takes the columns/field names.

header = ['Proper noun', 'M1 Score', 'M2 Score'] data = [['Alex', 62, fourscore], ['Brad', 45, 56], ['Joey', 85, 98]] information = pd.DataFrame(information, columns=header)

three. Write to a CSV file using to_csv()

Syntax: DataFrame.to_csv(filename, sep=',', index=False)

**separator is ',' by default.

alphabetize=Simulated to remove the index numbers.

information.to_csv('Stu_data.csv', index=False)

Below is how our CSV looks like

Cease Notes:

Give thanks you for reading till the conclusion. Past the stop of this article, we are familiar with different means of handling CSV files in Python.

I hope this article is informative. Feel free to share it with your report buddies.

References:

Cheque out the complete code from the GitHub repo.

Other Blog Posts past me

Feel free to check out my other blog posts from my Analytics Vidhya Profile.

You lot can find me on LinkedIn, Twitter in case you would desire to connect. I would be glad to connect with you.

For immediate exchange of thoughts, please write to me at harikabont[email protected].

The media shown in this article are not owned by Analytics Vidhya and are used at the Author's discretion.

Reading a Column From Csv File and Make Call to Api

Source: https://www.analyticsvidhya.com/blog/2021/08/python-tutorial-working-with-csv-file-for-data-science/