EME 210
Data Analytics for Energy Systems

(link is external) (link is external)

Importing Files: Option 2

PrintPrint

Importing Files: Option 2

 Read It: Importing Files

Here, we are continuing to explore means in which we can read CSV files into Google Colab. Here, we will use the "drag and drop" method. This does not require you to have your files stored on Google Drive, such as the previous example, but instead can be stored anywhere on your local computer.

  Watch It: Video - Importing Files: Option 2 (3:03 minutes)

Click here for a transcript.

Hi. In this video we're going to go over some basics of using Google Colab. In particular, inserting coding cells and running those computations. So, without further ado, here we go. So, we're in this practice document here. And to do my work, to insert the code, all I have to do is hover over one chunk of code and click, plus code. Okay, and so right now I've got a code cell. And I can enter any code I want here. The prompt for this exercise has me print out a statement, hello world. And so, I can type out my code to do that. In order to make this happen, we'll use the print function, which I will go over many functions in this course, so this is the first that you're introduced to. The print function will just print whatever argument we give it in the subsequent parentheses to the screen. And so, in quotes I'll put “hello world!” exclamation point and that's my instruction for the computation here.

In order to make this happen however, I need to click the play button. Pretty straightforward. Note that when I do this, it takes a minute because it's connecting up here to Google servers. When we get this green check mark it's connected, and it can execute. Here it's executed, and it's printed to the screen, hello world. Let's do something mathematical. So, if we want to insert a code cell down here, again hit code, also I can go to insert, and choose code cell up here. It'll be another way to do it. And so, the prompt here is to display the result of any mathematical operation. And so, if I just do two plus two, if I treat this like a regular calculator and just do two plus two, it'll print out four. Note that I don't have to use print here because it just gives the default output straight to this. However, if I assign this output to a variable by saying variable x for example, equals the result of this operation, it does not get displayed to screen unless I resort to my print command. Note that I do not use quotes here because this is not, I don't want to print literally x, I want to print what is contained in the object x, and it's 4. If I had used quotes it would print literally x to the screen. You see the difference there? Furthermore, some other basic operations are subtraction, using the minus sign, you get zero, of course. Division, you get one, multiplication, four, and exponentiation we also get four.

One other thing to point out here in terms of basics is, we can see what variables we have stored by looking at our variable list here. So, we click on this variable list. It opens up a side menu, and we see we have variable x stored as an integer here. If I make another variable, when I say, y equals four times eight, I print y to the screen. Not only am I left with what's printed for x, but I also have the output from y here, and we get y as a new variable here. Note that when I click play here, it reruns this whole cell in order from top to bottom. So, it does x first, prints x, does y second, prints y. Furthermore, the order that we run the cells, the coding cells, is important. So, if I insert another coding cell here, and I do another operation, I say z is equal to x plus y, we print z. Note that this is going to require x and y to exist before I run the cell. So, I need to run this one first, so that x and y are contained in memory. That is, I have them in the variable list here, and then I can run the second cell that has z, working with x, and y. And now we have the z variable. So, pay attention to the order that you run cells in, because if we restart runtime here, and wipe out what's in memory, if I now try to run this cell without having x and y, I get an error, “x is not defined.” And so, they know I'd have to pay attention and say, well I should have run this one first, so that I have x and y, and now I can do z.

Okay, in subsequent videos we'll go over the how to use the text cell and more about functions, but for now that gives you a good basic overview of how to use the coding cells in Google Colab.

Credit: © Penn State is licensed under CC BY-NC-SA 4.0(link is external)

  Try It: Apply Your Coding Skills in Google Colab

  1. The Google Colab file used in the video is linked here(link is external).
  2. 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.
  3. 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
import pandas as pd
 
df = pd.read_csv('yourfilename.csv')

Once you have implemented this code on your own, come back to this page to test your knowledge. 


  Assess It: Check Your Knowledge

Knowledge Check

frequently asked questions FAQ