How can I read CSV files with Pandas using the ultimate guide?

The ultimate guide to reading CSV files with Pandas is a comprehensive resource that provides step-by-step instructions for utilizing the Pandas library to efficiently and accurately read and manipulate CSV files. This guide offers a detailed explanation of the process, along with helpful tips and tricks, making it a valuable tool for anyone looking to work with CSV files using Pandas. By following this ultimate guide, individuals can easily and effectively access and analyze data from CSV files for a variety of purposes.

The Ultimate Guide: Read CSV Files with Pandas


CSV (comma-separated value) files are one of the most common ways to store data.

Fortunately the pandas function read_csv() allows you to easily read in CSV files into Python in almost any format you’d like.

This tutorial explains several ways to read CSV files into Python using the following CSV file named ‘data.csv’:

playerID,team,points
1,Lakers,26
2,Mavs,19
3,Bucks,24
4,Spurs,22

Example 1: Read CSV File into pandas DataFrame

The following code shows how to read the CSV file into a pandas DataFrame:

#import CSV file as DataFrame
df = pd.read_csv('data.csv')#view DataFrame
df

        playerID  team	  points
0	1	  Lakers  26
1	2	  Mavs	  19
2	3	  Bucks	  24
3	4	  Spurs	  22

Example 2: Read Specific Columns from CSV File

The following code shows how to read only the columns titled ‘playerID’ and ‘points’ in the CSV file into a pandas DataFrame:

#import only specific columns from CSV file
df = pd.read_csv('data.csv', usecols=['playerID', 'points'])#view DataFrame
df

	playerID  points
0	1	  26
1	2	  19
2	3	  24
3	4	  22

Alternatively you can specify column indices to read into a pandas DataFrame:

#import only specific columns from CSV file
df = pd.read_csv('data.csv', usecols=[0, 1])#view DataFrame
df

        playerID  team
0	1	  Lakers
1	2	  Mavs
2	3	  Bucks
3	4	  Spurs

Example 3: Specify Header Row when Importing CSV File

In some cases, the header row might not be the first row in a CSV file.

For example, consider the following CSV file in which the header row actually appears in the second row:

random,data,values
playerID,team,points
1,Lakers,26
2,Mavs,19
3,Bucks,24
4,Spurs,22

To read this CSV file into a pandas DataFrame, we can specify header=1 as follows:

#import from CSV file and specify that header starts on second row
df = pd.read_csv('data.csv', header=1)#view DataFrame
df

        playerID team	points
0	1	 Lakers	26
1	2	 Mavs	19
2	3	 Bucks	24
3	4	 Spurs	22

Example 4: Skip Rows when Importing CSV File

You can also easily skip rows when importing a CSV file by using the skiprows argument.

For example, the following code shows how to skip the second row when importing the CSV file:

#import from CSV file and skip second row
df = pd.read_csv('data.csv', skiprows=[1])#view DataFrame
df

        playerID team	points
0	2	 Mavs	19
1	3	 Bucks	24
2	4	 Spurs	22

And the following code shows how to skip the second and third row when importing the CSV file:

#import from CSV file and skip second and third rows
df = pd.read_csv('data.csv', skiprows=[1, 2])#view DataFrame
df

        playerID team	points
1	3	 Bucks	24
2	4	 Spurs	22

Example 5: Read CSV Files with Custom Delimiter

Occasionally you may have a CSV file with a delimiter that is different from a comma.

For example, suppose our CSV file has an underscore as a delimiter:

playerID_team_points
1_Lakers_26
2_Mavs_19
3_Bucks_24
4_Spurs_22

To read this CSV file into pandas, we can use the sep argument to specify the delimiter to use when reading the file:

#import from CSV file and specify delimiter to use
df = pd.read_csv('data.csv', sep='_')#view DataFrame
df

	playerID team	points
0	1	 Lakers	26
1	2	 Mavs	19
2	3	 Bucks	24
3	4	 Spurs	22

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

x