
Importing Files: Option 1
Read It: Importing Files
Introducing Comma Separated Value files
In this course, you will primarily work with CSV (comma-separated value) files. You can recognize these files in your computer file system as xxxxxxx.csv files, with the “.csv” extension. These files can be easily opened with spreadsheet software, such as Microsoft Excel or Google Sheets. Technically, they can even be opened by Microsoft Word or basic text editors. However, they may not look like a table in those formats! Below we have some examples of what a CSV file looks like in Excel, Word, and a generic text editor.
CSV files are also easy to read into nearly every programming language. This means that if you're working in Python, you can read in a CSV file, make some changes, write to a new CSV file, and send the new file to a colleague working in R or MATLAB. If it is in CSV format, they will be able to open the same file and see what you did without any issues going between the languages.
How to read files into Google Colab
When you are using Python, you can read other file types beyond CSV files. For example, Python can read an Excel spreadsheet (“.xls” or “.xlsx” extensions) or a text file (“.txt” extension). If you get into more advanced coding, you can read in GIS files or Google Maps files. In Python, you can read nearly any file type that you come across, but in this class, we'll be using CSV files.
Occasionally, you may come across data that is in Excel format (“.xlsx” extension), but want to read it into Python as a CSV file. In this case, you can easily convert your file using the “Save As...” feature, shown below.
Once you have your data in CSV format, you need to upload that data to Colab. To do this, we will be using the Pandas library(link is external), which was introduced previously in this lesson. In this lesson, we will be demonstrating three main ways to import CSV files into Colab, the first of which uses your existing Google Drive. Note that in order to use this method of uploading files, you will need to have your CSV file of interest stored on your Google Drive prior to running the code.
Watch It: Video - Importing Files: Option1 (5:44 minutes)
Try It: Apply Your Coding Skills in Google Colab
- The Google Colab file used in the video is linked here(link is external).
- Go to the Colab file and click “File” then “Save a copy in Drive”, this will create a new Colab file that you can edit in your own Google Drive account.
- Once you have it saved in your Drive, try to implement the following code to import a file of your choice by mounting your Google Drive:
Note: You must be logged into your PSU Google Workspace in order to access the file.
1 2 3 4 5 6 7 8 9 | from google.colab import drive drive.mount( '/content/drive' ) import pandas as pd df = pd.read_csv( 'yourfilename.csv' ) df # print the dataframe |
Once you have implemented this code on your own, come back to this page to test your knowledge.