Loading CSV files in Google Colaboratory with Pandas
Check the current directory:
In Google Colab, you can check the current directory by running Python code within a code cell. Google Colab provides a code execution environment similar to Jupyter notebooks. Here's how you can check the current directory:
Open a new or existing Colab notebook.
Now, there are two ways to check for the current directory:
1. Using Shell Commands
- Check the current Directory using (pwd)
Using ‘pwd’, you can know your current Directory as follows:
Now, we know that we are on a directory called ‘/content’
- List Content with (ls)
Using the ‘ls’ , you can get the list of files and folders in your current directory, as follows:
To list files inside sample_data, just run ‘ls sample_data’
From this list, we will select the file ‘california_housing_train.csv’. The full path will be “/content/sample_data/california_housing_train.csv”
Now that we made sure we have a CSV file to load, let’s create the DataFrame from it as follows:
- Change current Directory using (cd)
You can change directory using (cd) as follows:
Now you do not need to put the entire path to open the file:
2. Using OS in Python
In a code cell, you can use the os module to check the current directory. Here's an example:
Run the code cell by clicking the "Run" button (or using the Shift+Enter keyboard shortcut).
After running the code cell, you'll see the current directory printed in the output below the code cell. It will typically be set to the root directory of your Google Drive where your Colab notebooks are stored, but you can change it using os.chdir() if needed.
Keep in mind that the current directory in Colab may vary depending on the specific environment and the notebook's location in Google Drive. If you want to access files or directories from a different location, you may need to navigate to that directory using os.chdir() or specify the full path when working with files.
List Content of Current Folder
To see the contents of the current directory in Google Colab, you can use the os module in Python to list the files and directories in the current working directory. Here's how to do it:
1. Open a Colab notebook.
2. In a code cell, use the following code to list the contents of the current directory:
3. Run the code cell by clicking the "Run" button (or using the Shift+Enter keyboard shortcut).
This code will first display the current directory's path and then list all the files and directories in that location. The list of contents will be displayed in the output below the code cell.
Please note that the contents of the current directory will depend on where your Colab notebook is located and any previous actions you've taken within the notebook. You can use the os.chdir() function to change the current directory if needed before listing its contents.
Change the current directory
You can change the current directory in Google Colab using the os module in Python. Here's how you can change the current directory:
1. Open a Colab notebook.
2. In a code cell, use the following code to change the current directory to a new path:
Replace "/content/sample_data" with the actual path of the directory you want to switch to. This path exists with the current release of Gogle Colab and this is where you will find the sample files.
3. Run the code cell by clicking the "Run" button (or using the Shift+Enter keyboard shortcut).
After running the code cell, the current directory will be changed to the specified path, and the new current directory path will be printed as output to verify the change.
You can now work with files and directories within the new directory by referencing them with their relative paths or using the os.listdir() function to list the contents of the new current directory.
Let's load the file california_housing_train.csv for use with Pandas
To load a CSV file named california_housing_train.csv from the current directory using Python and Pandas in Google Colab, follow these steps:
1. Make sure you have changed the current directory to the location where the CSV file is stored. If you haven't already, use the code provided in a previous response to change the current directory.
2. Once you're in the correct directory, you can use Pandas to load the CSV file. Here's an example code snippet:
3. Run the code cell by clicking the "Run" button (or using the Shift+Enter keyboard shortcut).
This code will load the california_housing_train.csv file into a Pandas DataFrame called df. You can then work with the data in the DataFrame, and df.head() will display the first few rows of the loaded data for you to verify that the file has been successfully loaded.
Make sure that the CSV file is in the current directory or provide the full path to the file if it's located in a different directory.
Upload Files to Google Colab:
To upload a file to Google Colab, you can follow these steps:
Open a Notebook: First, open a Google Colab notebook or create a new one if you haven't already.
Mount Google Drive (optional): If your file is in Google Drive, you can mount your Google Drive to access files directly. This step is optional, but it can be convenient for accessing files stored in your Google Drive.
Follow the link provided, sign in to your Google account, and grant the necessary permissions. Afterward, you'll get a verification code to enter in the cell.
Upload a File: To upload a file directly to your Colab environment, use the following code. Replace 'your_file_path' with the path to the file you want to upload:
When you run this cell, you'll see a button that allows you to browse your local machine for the file you want to upload. After selecting the file, it will be uploaded to your Colab environment, and you'll get a confirmation message showing the file name and size.
Access the Uploaded File: You can access the uploaded file as follows:
Remember that files uploaded using this method are temporary and will be removed once your Colab session ends. If you want to access the same files in future sessions, you may want to save them to Google Drive or use a more permanent storage solution.
You can download files from
Now. let's use this file:
Using the Browser
You can achieve all the previous using the mouse on the browser as follows:
Upload Files:
File Path
You can always copy the full path from the side menue as follows: