Locked lesson.
About this lesson
In this video, we cover how to add a new column to your data frame.
Exercise files
Download this lesson’s related exercise files.
Creating New Columns.docx57 KB Creating New Columns - Solution.docx
55.4 KB
Quick reference
Creating New Columns
It's easy to add new columns to existing DataFrames.
When to use
Use this whenever you want to add another column - or multiple columns - to an existing dataframe.
Instructions
To add a column named "Sun" with 4 rows and 1 column:
my_df["Sun"] = randn(4,1)
To do math on columns and output the result to a new "Total" column:
my_df["Total"] = my_df["Mon"] + my_df["Tues"]
Hints & tips
- my_df["Sun"] = randn(4,1)
- my_df["Total"] = my_df["Mon"] + my_df["Tues"]
- 00:05 Okay so, we created our data frame we've got Monday, Tuesday, and Wednesday, and
- 00:09 we're feeling pretty good about ourselves because it's looking pretty good here now.
- 00:13 But then we realize, we made a mistake, we need a Sunday column too,
- 00:17 how do we add an extra column once our data frame has already been created?
- 00:21 Well, that's what we're going to do in this video, and it's actually really,
- 00:24 really simple.
- 00:24 We can just come down here and go my_df, and
- 00:28 then pass in whatever we want to create.
- 00:31 So let's create a column called Sun, and
- 00:34 then we just want to set this equal to whatever we want.
- 00:37 So we can come up here to our see this randn guy that we did earlier,
- 00:42 and just copy this code, and let's generate some more random data.
- 00:48 Now remember, this is rows and columns, so we need four rows, but
- 00:54 we only want one column because we're creating one column called Sun.
- 00:59 And so we can do that, hit Shift+Enter to run this.
- 01:02 Now if we call our data frame again, we see now we've got Sun, so
- 01:07 very cool and very, very simple.
- 01:10 So that's one way you can do it.
- 01:12 Now, a lot of times when you want to create new columns,
- 01:14 it's because you want to do something, you want to add up all your columns.
- 01:18 So you want to find the average of all your columns, or you want to find the min
- 01:21 or the max of all your columns, or have all your data in each column.
- 01:25 So how do we do that?
- 01:26 Well, let's create another column, and let's call it Total.
- 01:30 So we just go through the same process we just did, we just go my_df, and
- 01:34 then just pass in whatever we want the column header to be right?
- 01:38 And then set this equal to whatever we want.
- 01:41 So let's say we want to add up Monday, Tuesday, and
- 01:46 Wednesday, and output that into the Total, or
- 01:50 we could just call my_df and then Mon, and then +, right?
- 01:56 So we can go my_df, and then we want to pass Tues,
- 02:03 and then again, + my_df and then Wed.
- 02:09 Remember these are case sensitive, so if it's W in Wednesday,
- 02:13 you need to type in W here.
- 02:15 So if we Shift+Enter, and run this, and
- 02:18 now pull up our my_df again, we see this, new Total.
- 02:23 And is this a correct number?
- 02:25 I don't know, let's go ahead and confirm that.
- 02:27 So I'm just going to copy and paste all this stuff down here and
- 02:31 let's just do some quick math.
- 02:33 We can do this in Jupyter Notebooks, this is not a Pandas thing,
- 02:36 this is just Jupyter Notebooks in Python, we can add, right?
- 02:40 So if we Shift run to see what this is,
- 02:44 it's -1.52263, whatever, and
- 02:49 here we see -1.52263, whatever.
- 02:54 So it looks like that's correct.
- 02:56 So we did adding, you could just as easily do subtraction, right?
- 03:04 Or multiplication, or whatever you want.
- 03:06 And as we go through the course, we'll learn about how to do different things
- 03:10 like finding the mean, finding the average, finding the min, the max,
- 03:14 the standard deviations, all the cool statistical things you would want to do.
- 03:18 But it's just as easy to create new columns and start doing stuff.
- 03:22 So very cool, let's get rid off of this just to clear things a bit, and
- 03:26 that's all there's to it.
- 03:28 Now, we created this Sun column, may be we didn't want to create that Sunday column.
- 03:34 We've got Monday, Tuesday and Wednesday and we're adding them all together and
- 03:37 outputing the total in this Total column,
- 03:40 we just sort of forgot about this Sun column.
- 03:42 So okay, we've decided this is a mistake, how do we get rid of this Sun column?
- 03:46 Well, we'll look at that in the next video.
Lesson notes are only available for subscribers.