Hello and welcome to a video on Python. This assignment will ask you to create variables.
Variables in Python are like a container for information. For example, we could use a variable called
name to hold information about a name. So here I'm going to set a variable called
name equal to Jane. Because we're using a text string, I'm going to enclose it in quotation marks.
Next we'll print name to our results. I'll click this button here to run the
code. When you first open a notebook, it
may take a moment for it to actually run your commands. But here you see below,
we've printed the name Jane. I could change this to something else,
and now it prints Smith. We'll change it back to Jane. This is a
statistics course, however. So we're usually more concerned with variables that contain numbers.
Here I've created a variable called var1, and I've set it equal to 57. We'll
print var1 below. In Python, two stars, two asterisks,
means raised to the power of when using it as an operation. So if I say 23 asterisks
asterisks 2, it will raise it to the power of 2 and we'll square it.
We'll save that in a variable called var2 and we'll print it below. A division
symbol means divide. So 23 forward slash 2 will
divide 23 by 2. We'll save that in a variable called var3 and print it below.
We can also use order of operations here too. So if we wanted to do them in a certain order, we could
do something like var4 equals parentheses 23 divided by 2, close parentheses,
all raised to the power of 2, and that will print below as var4. Let's go ahead and
print everything out. So you can see we've set 57 to 57. There we go.
23 raised to the power of 2. 23 squared is 529. 23 divided by 2
is 11.5 and 23 divided by 2 raised to the power of 2. So 11.5
times 11.5 is 132.25. If you've done a little
programming before, you may have noticed I didn't have to tell Python that var1 is a number. It will figure out what
kind of variable it is based on the information you put in it. That's good. Maybe we want to enter
in a string of numbers. We call this string of numbers a list. A list doesn't have to contain numbers.
In fact, we can make a list of just about anything. In this case, we'll make a list called
var. And I'm just going to put in some random numbers here. Let's print that out.
This might be pretty useful. What if we wanted to multiply every number in var by var 1? In other
words, we want to multiply 57 times 2, then 57 times 5, then by 2,
etc. If we try to simply multiply the two together, let's see what happens.
So vars_mult, meaning I'm multiplying vars together. And you can name your variables, whatever
you want, so long as they're sensible and make sense to you and whoever's reading your code, equals
vars times var1. The asterisk is the multiplication symbol in Python.
We'll print that out as vars_mult. Well, that's not what we wanted, right?
What Python has done instead is take this list and repeat it 57 times. Maybe this
would be useful in other circumstances, but not this time. This is where pandas, a package in Python,
comes in. Let's try to use it to solve this problem. First, we'll need to import pandas.
You may need to install pandas first. Do that with the following line. I'll take the hashtag off
of it. It's exclamation point pip and that's what you use in front of any installation you're doing.
Install pandas. Once you've installed pandas, you don't need to do it again.
Now, this next line, letss Python know we want to use pandas and create an abbreviation for us to use so we aren't
typing pandas over and over. We'll import pandas as PD. So we're
telling Python import this library so that I can use it in my code.
We'll create a new variable called DF to stand for data frame. When we enter this data,
the A part of this code is for the name of the column. So DF equals
PD.dataframe. So data frame here is a function from pandas that
we're using. These words will make more sense the longer you code and get used to hearing
them and seeing them. We're opening a set of parentheses and then inside that parentheses,
we're opening a set of brackets. A is going to be the name of this column where
we have these numbers attached to it. So let's print DF.
Okay, so this first column you see here is our index. The index and Python always starts
at zero. So we know that we have nine numbers here, not just eight.
The A column here is exactly what we named it above and we could have named this anything. We could
have changed this name to puppy or a var one or time in seconds.
Whatever it is we were trying to measure or capture. You want this name to reflect
what that item is so that it makes more sense in your code. In this case, we're just going to call it A.
Let's create a new column called B by multiplying A by var one that we created earlier.
So up here in our code, we've already run this section of code where we created var one. That
means that it's available to us to use down here in this new section of code where we're calling
var one. If we hadn't run that first section of code, we would get an error saying that
that variable does not exist. So first, we're going to create our new data frame
and we're going to set it um set the name as b. So df
open braces b equals df open braces a
and this is how we refer to different variables in a data frame. Multiply by var one and then
let's print our new data frame and you can see we have a just as we did before but now
we have a new column called b where all of those variables were multiplied by 57.
So that's more like it. We've already kind of talked about what a data frame is in pandas but we
can do all sorts of useful things with data frames. Now what if we have a data frame already that
we want to import into colab to use with pandas? Let's go ahead and try that now. First, make
sure you have the data you want to use saved in your Google Drive. We'll use the classdataS20 file that
you'll use for your homework as an example. We've already mounted pandas above.
So in order to do this, you would want to say import pandas as PD in
addition to from google.colab import drive. You have to have both
working at the same time for this to work.
Next we have a line where we're mounting our Google Drive and this will be the same for everyone.
It's just drive.mount/content/drive. Make sure to put
your data in a folder called statlab all lowercase and move your copy of the class
data into it. That way your file path will look the exact same as this.
So you'll set your file path and it will be equal to open
"/content/drive/MyDrive/statlab/classdataS20.xlsx".
Next, we'll create a data frame called DF. You can name your data frame anything so if you wanted to name
it class data or something else that just makes more sense to you, please feel free to do so.
And we'll set that equal to PD, so pandas, we're using another pandas function. Read
Excel, and then we're going to set that argument to file path right here. An argument is
something that goes inside the set of parentheses after your function. We'll talk more about arguments in a later assignment.
Next, we'll print DF.head, which is the top of
our data frame. This first time that I'm running
this, it's going to ask if if I want to give it permission to connect to Google Drive. I'm going to say connect to Google
Drive. I'll select my email address or your UALR address is what you want to
select and hit continue and then hit continue and this
will take it just a moment to do.
There we go. So we can see we have three variables, IV1, IV2, and DV.
So that was a lot of steps. Basically, we told Python. We'd like to access a file on our Google Drive and
we created a file path to the exact file we wanted to access. Finally, we used pandas to read
the Excel file using that file path. Pretty good. Let's see if we can make some new variables now.
You'll notice these already have names. So instead of referring to them as A or B, we'll call
them IV1, IV2, and DV. What if we want to multiply IV1 and
IV2 together? Let's call this variable IV3.
So df['IV3] = df['IV1'] * df['IV2'].
And we'll go ahead and print the first couple of results from that data frame.
Okay, so as expected, these are some pretty big numbers. These were multiplying 80 times
88.5 and getting 7,880. But you
can see that it multiplies each one down the row. So 78 times 93, 7254, 80
times 80, 6400. And you can just sort of manually check these if you feel like you want to to make sure
that what you're doing is what you think you're doing. Usually if you're getting the wrong
results here, it is a person error and not a Python error. So something
you've done in the code is wrong or your calculations are off or something like that.
We could also use that variable we created earlier, var1. So we've created that constant remember
of 57. Let's divide IV3 by var1 and make a new variable IV4.
So df IV4 equals df IV3 divided by var1 and we'll go ahead and
print the first few results as that.
So now you can see we've taken IV3 and divided it by 57 and we've ended up with this
column of numbers here. Nice. That should be all you need to get started on your first
homework assignment. Please let us know if you have any questions about this material and happy coding.