📣 Introducing $2.95/Hr H100, H200, B200s, and B300s: train, fine-tune, and scale ML models affordably, without having to DIY the infrastructure   📣 Run Saturn Cloud on AWS, GCP, Azure, Nebius, Crusoe, or on-prem. 📣 Introducing $2.95/Hr H100, H200, B200s, and B300s: train, fine-tune, and scale ML models affordably, without having to DIY the infrastructure   📣 Run Saturn Cloud on AWS, GCP, Azure, Nebius, Crusoe, or on-prem. 📣 Introducing $2.95/Hr H100, H200, B200s, and B300s: train, fine-tune, and scale ML models affordably, without having to DIY the infrastructure   📣 Run Saturn Cloud on AWS, GCP, Azure, Nebius, Crusoe, or on-prem.
← Back to Blog

How to Specify File Path in Jupyter Notebook

As a data scientist or software engineer, you often work with large amounts of data, and accessing this data is essential. One of the most common tools for data analysis is Jupyter Notebook, an interactive coding environment that allows you to write and execute code in a web browser. In Jupyter Notebook, you can easily import data from various sources, including CSV, Excel, JSON, and SQL. However, to import data from these sources, you need to specify the file path correctly. In this blog post, we'll look at how to specify file paths in Jupyter Notebook.

How to Specify File Path in Jupyter Notebook

How to Specify File Path in Jupyter Notebook

As a data scientist or software engineer, you often work with large amounts of data, and accessing this data is essential. One of the most common tools for data analysis is Jupyter Notebook, an interactive coding environment that allows you to write and execute code in a web browser. In Jupyter Notebook, you can easily import data from various sources, including CSV, Excel, JSON, and SQL. However, to import data from these sources, you need to specify the file path correctly. In this blog post, we’ll look at how to specify file paths in Jupyter Notebook.

What is a File Path?

A file path is a string that identifies the location of a file or folder in a file system. A file path can be absolute or relative. An absolute file path specifies the exact location of a file or folder from the root directory, while a relative file path specifies the location of a file or folder relative to the current directory.

Absolute File Path

To specify an absolute file path in Jupyter Notebook, you need to provide the full path to the file or folder. For example, if you have a file named data.csv located in the directory C:\Users\johndoe\Documents\, the absolute file path would be C:\Users\johndoe\Documents\data.csv.

In Jupyter Notebook, you can use the pd.read_csv() function from the Pandas library to read a CSV file. To specify an absolute file path, you can pass the file path as a string to the pd.read_csv() function. Here’s an example:

import pandas as pd

data = pd.read_csv('C:\Users\johndoe\Documents\data.csv')

Relative File Path

To specify a relative file path in Jupyter Notebook, you need to provide the path to the file or folder relative to the current directory. The current directory is the directory in which the Jupyter Notebook file is located.

In Jupyter Notebook, you can use the os module to get the current directory. The os.getcwd() function returns the current working directory as a string. Here’s an example:

import os

current_dir = os.getcwd()

Suppose you have a file named data.csv located in the directory C:\Users\johndoe\Documents\. If your Jupyter Notebook file is located in the directory C:\Users\johndoe\Documents\project\, the relative file path to data.csv would be ..\data.csv.

In Jupyter Notebook, you can use the pd.read_csv() function from the Pandas library to read a CSV file. To specify a relative file path, you can pass the file path as a string to the pd.read_csv() function. Here’s an example:

import pandas as pd

data = pd.read_csv('..\data.csv')

Conclusion

In this blog post, we’ve looked at how to specify file paths in Jupyter Notebook. A file path is a string that identifies the location of a file or folder in a file system. To specify an absolute file path in Jupyter Notebook, you need to provide the full path to the file or folder. To specify a relative file path, you need to provide the path to the file or folder relative to the current directory.

When working with large amounts of data, it’s essential to access the data quickly and efficiently. By specifying file paths correctly in Jupyter Notebook, you can easily import data from various sources and perform data analysis.

Keep reading

Related articles

How to Specify File Path in Jupyter Notebook
Dec 29, 2023

How to Resolve Memory Errors in Amazon SageMaker

How to Specify File Path in Jupyter Notebook
Dec 22, 2023

Loading S3 Data into Your AWS SageMaker Notebook: A Guide

How to Specify File Path in Jupyter Notebook
Dec 19, 2023

How to Convert Pandas Series to DateTime in a DataFrame