How to read a CSV file in Python using Pandas

1 min readDataPandasDataFrame
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

CSV files are another industry standard.

They are usually used to store and share table like data.

For example, Microsoft Excel can natively save Worksheets into CSV files.

In Python you can easily work with CSV files.

The Pandas library is often used when working CSV files and Dataframes in general.

Pandas has a really powerful method to manipulate CSV.

Here is an example

import pandas as pd # We import the pandas library

df = pd.read_csv("path/to/file.csv", sep=",") # We read the csv file

print(df) # We print the dataframe

Here we only specify the path to the file and the separator (sep) but you can add some other parameters listed on the documentation here.

The most useful I've found are the following:

  1. sep : The character used to separate columns in the csv file. By default it's "," (e.g. Sheet1)
  2. headers : Usually the row where are the columns names (e.g. 0)
  3. index_col : The index of the columns we want as index. (e.g. 0)
  4. skiprows : Whether we should skip any rows (e.g. 1)
  5. engine : which engine to use. It can be either c or Python, If you are working with large files, "c" might be more adequate (e.g. "c")

Here you are ! You can now read csv files in Python !

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.