Move from Excel to Python with Pandas Transcripts
Chapter: Intro to Pandas
Lecture: Working with multiple columns
0:00 So now the next thing might be Well,
0:02 how do you work with multiple columns?
0:07 Let's get some summary information on the price and quantity columns.
0:11 So one way to do this if we want to work with multiple columns,
0:14 is we define a variable and we need to use a list.
0:18 And then if we want most about columns and then if we want to get let's
0:24 say, the average value for the price and quantity we can do df["summary_columns"]
0:29 and this is going to calculate the mean we could ... if we wanted to
0:35 You could do -sum. Sum may not really be useful in this context,
0:39 but let's go and stick with mean.
0:41 So that's one way that you can combine multiple columns together and then let me show
0:46 you that you can define a variable like that,
0:49 and I think that's a good practice.
0:50 But if you choose not to define a variable,
0:54 and I'm cutting and pasting on purpose so you can kind of see how this works.
0:57 I just defined that list of columns that I want to work on,
1:01 and it can do that as well,
1:03 so you'll see both methods when you are looking at your pandas data and looking at
1:09 examples online. I want to go over one Other example.
1:15 If you want to use the describe function,
1:18 you can do that as well.
1:22 And remember, we ran that on full data frame once again have chosen just a
1:26 small set of data frames and then the final thing.
1:30 Let's go through a real quick example of how we would add a new column.
1:36 So let's say we want to put a country column on here,
1:40 and we know that everyone is in the US.
1:42 So think about how you do that.
1:44 If you had an Excel bio,
1:46 you would probably put us a in a columm and drag it down.
1:49 Here. You just assign the string USA to that country column.
1:54 And now if you say df.head(),
1:56 it's gonna show country for all the values.
1:58 And if you want to check,
2:00 you can look at df.tail() and see that country is everywhere.
2:03 So we can also do something similar if we want to do math.
2:06 So let's say we have a 15% or a,
2:09 1.5% fee. We want to add we can,
2:12 Say Okay, let's add df.['fee'] = df.['extended amount'] * .015
2:17 So what this does is adds a new column called Fee.
2:20 It is taking the "extended amount" times 0.15 and adding it as the entry in the
2:27 fee column. So we press enter.
2:29 And now if you look at the column,
2:32 you can see the fee. So this is "compare this" to how you would create
2:37 a formula in Excel, where you would have to create that formula and drag it
2:41 down for each row. Here you just enter it once,
2:44 and pandas takes care of making sure that everybody gets that value.
2:48 So it's a really compact and simple way to analyze things.
2:52 It also makes it easy to troubleshoot because you're only putting that formula in one location