In your career as a data professional, you will come across various datasets that have different file types or are stored in various databases. As you’ve learned previously, it is critical for you to know what these data types are and how to import data using Python. Below you will find examples of importing both databases through connections and data files into Python.

Although you will use the Coursera platform for Python coding, you will need to know how to work with and import CSV files if you’d like to download and open them outside of Coursera.

How to import a dataset from a CSV file

For this example, find a CSV file on your computer. If you don't have one, you can use a dataset of unicorn companies (companies that reached a valuation of $1 billion USD) from this course's Resources Opens in a new tab .

There are several different ways to import a CSV file into Python, but we will only review some of the more common ways. Start by using a with statement and open() function. Pass the file name (or file path) of the CSV file to the open() function along with an argument for the mode parameter of the function.

with open(‘file_path/file_name, mode=)

The mode is telling the Python library what to do with the file. When defining the mode, you use one of the following options:

Typically, you’ll be defining the mode inside the with open() argument field as 'r' because you want Python to open and read the CSV file.

Next, we’ll add as file to the end, which is assigning the result to a variable name. In this case, we’ll name it data.

with open('example_filepath/file', mode='r') as file:
	data = file.read()

Importing a CSV file using pandas

Instead of using Python's standard library to read a file, you can use pandas to import the CSV file into a dataframe. First, of course, you’ll want to import the pandas library into your Python notebook.

import pandas as pd

Next, you’ll use the read_csv() function to load the data into a dataframe. The file path then goes in the argument field.

df = pd.read_csv('file_path/file_name')