Dealing with CSV Files

In this lesson, we discuss how to read from and write to CSV files.

We'll cover the following

CSV

CSV stands for comma separated values. These are the most common and widely used files with a pandas DataFrame object. It is a kind of a text file that uses the extension .csv instead of .txt. Text is separated by commas , in this file, and when a DatFrame reads this file, values between commas become cell values of the DataFrame.

The pandas package provides both reading and writing functions for csv files.

Reading CSV files

The read_csv(file_name) function is used to read from a csv file. The file_name should be replaced with the name of your file. Make sure that the file is in the same directory as your program file or clearly define the path to that file, e.g., folder/folder/file.csv. If no path is defined with the filename, then the program looks for the file in the current directory.

Get hands-on with 1000+ tech skills courses.