After Reading a Tsv File in Python, How Do Handle Rows

Intro: In this article, 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 grade of storing data in tabular class as plain text. It is of import to know to piece of work with CSV considering nosotros mostly rely on CSV data in our day-to-twenty-four hour period lives as data scientists.

Construction 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.

After the header, each line of the file is an ascertainment/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.

ii.1 Using csv.reader

Reading a CSV using Python'due south inbuilt module chosen csv using csv.reader object.

Steps to read a CSV file:

i. 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 past the open up() method.

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

csvreader = csv.reader(file)

4. Extract the field names

Create an empty list called header. Use the next() method to obtain the header.

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

The first time you lot run side by side() information technology returns the header and the next time you run it returns the start record so on.

header = [] header = next(csvreader) header

5. Extract the rows/records

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

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

half-dozen. Close the file

.shut() method is used to close the opened file. In one case it is closed, we cannot perform any operations on information technology.

file.close()

Complete Lawmaking:

import csv file = open up("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, nosotros might forget to shut an open file. To avoid that nosotros can utilize the with()statement to automatically release the resources. In simple terms, in that location is no need to call the .shut() method if we are using with() argument.

Implementing the above code using with() statement:

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

Modes:

'r' – to read an existing file,
'w' – to create a new file if the given file doesn't be 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 up("Salary_Data.csv", 'r) equally file:     csvreader = csv.reader(file)     header = next(csvreader)     for row in csvreader:         rows.suspend(row) print(header) print(rows)

2.2 Using .readlines()

At present the question is – "Is it possible to fetch the header, rows using only open up() 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 item of the listing is a row of our CSV file.

The offset row of the file.readlines() is the header and the balance of them are the records.

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

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

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

Here comes the pandas library into the picture.

2.3 Using pandas

Steps of reading CSV files using pandas

1. Import pandas library

import pandas as pd

2. Load CSV files to pandas using read_csv()

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

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

3. Extract the field names

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

information.columns

4. Extract the rows

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

data.Salary

3. Writing to a CSV file

Nosotros can write to a CSV file in multiple means.

3.ane Using csv.writer

Let'due south assume we are recording 3 Students data(Name, M1 Score, M2 Score)

header = ['Proper name', 'M1 Score', 'M2 Score'] data = [['Alex', 62, fourscore], ['Brad', 45, 56], ['Joey', 85, 98]]

Steps of writing to a CSV file:

1. Import csv library

import csv

two. Ascertain a filename and Open the file using open()

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

4. Write the header

5. Write the rest of the data

code for steps 2-five

filename = 'Students_Data.csv' with open(filename, 'west', newline="") as file:     csvwriter = csv.writer(file) # 2. create a csvwriter object     csvwriter.writerow(header) # 4. write the header     csvwriter.writerows(data) # five. write the balance of the data

Below is how our CSV file looks.

3.ii Using .writelines()

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

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

3.3. Using pandas

Steps to writing to a CSV using pandas

1. Import pandas library

import pandas equally pd

2. Create a pandas dataframe using pd.DataFrame

Syntax: pd.DataFrame(data, columns)

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

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

three. Write to a CSV file using to_csv()

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

**separator is ',' by default.

index=False to remove the alphabetize numbers.

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

Beneath is how our CSV looks like

Terminate Notes:

Thank you for reading till the conclusion. By the finish of this article, we are familiar with dissimilar ways of handling CSV files in Python.

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

References:

Bank check out the complete code from the GitHub repo.

Other Web log Posts by me

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

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

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

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

deeseeale1964.blogspot.com

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

0 Response to "After Reading a Tsv File in Python, How Do Handle Rows"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel