Reading & Write Files
Learning Objectives
Reading a data file
Writing a data file
Reading Data Files
It is always good to be able to create a dataframe by hand. But, generally, we don’t create our own data by hand. We work on the data that already exists.
Data exists in number of formats. The most basic of these is the csv file. csv stands for comma-separated-value
What is a CSV file?
CSV files are normally created by programs that handle large amounts of data. They are a convenient way to export data from spreadsheets and databases as well as import or use it in other programs.
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database.
A CSV file stores tabular data (numbers and text) in plain text.
Each line of the file is a data record.
Each record consists of one or more fields, separated by commas.
The use of the comma as a field separator is the source of the name for this file format.
How does CSV look like?
Working with CSV files in Python
For working CSV files in python, there is an inbuilt function named read_csv.
However, a common method for working with CSV files is using Pandas. It makes importing and analyzing data much easier.
One crucial feature of Pandas is its ability to write and read Excel, CSV, and many other types of files.
Pandas read_csv
Functions like the Pandas read_csv() method enable you to work with files effectively.
The read_csv() function reads the CSV file into a DataFrame object.
A CSV file is similar to a two-dimensional table and the DataFrame object represents two dimensional tabular view.
The most basic way to read a csv file in Pandas:
Now, let's understand how to provide filename
There are many other things one can do through this function only to change the returned object completely.
For instance, one can read a csv file not only locally, but from a URL through read_csv or one can choose what columns needed to export so that we don’t have to edit the array later.
These modifications can be done by the various arguments it takes.
We don’t need to memorise all the arguments though, let’s have a look at few important ones in the next two slides.
Pandas to_csv with example
The easiest way to write DataFrames to CSV files is using the Pandas to_csv function.
Syntax:
If you want to export without the index, simply add index=False
Example:
Last updated
Was this helpful?