Introduction to Pandas - Basic Operations

Python 3: Automating Your Job Tasks Superhero Level: Automate Data Analysis Tasks with Python 3
9 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.10
List Price:  €128.94
You save:  €36.84
£77.67
List Price:  £108.74
You save:  £31.07
CA$138.12
List Price:  CA$193.37
You save:  CA$55.25
A$152.38
List Price:  A$213.34
You save:  A$60.96
S$134.35
List Price:  S$188.10
You save:  S$53.74
HK$780.56
List Price:  HK$1,092.81
You save:  HK$312.25
CHF 88.35
List Price:  CHF 123.69
You save:  CHF 35.34
NOK kr1,098.66
List Price:  NOK kr1,538.17
You save:  NOK kr439.51
DKK kr687.32
List Price:  DKK kr962.28
You save:  DKK kr274.95
NZ$169.58
List Price:  NZ$237.42
You save:  NZ$67.84
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.91
৳11,746.62
List Price:  ৳16,445.74
You save:  ৳4,699.11
₹8,371.76
List Price:  ₹11,720.80
You save:  ₹3,349.03
RM465.70
List Price:  RM652
You save:  RM186.30
₦159,584.04
List Price:  ₦223,424.04
You save:  ₦63,840
₨27,830.93
List Price:  ₨38,964.42
You save:  ₨11,133.48
฿3,601.18
List Price:  ฿5,041.80
You save:  ฿1,440.62
₺3,299.80
List Price:  ₺4,619.86
You save:  ₺1,320.05
B$564.45
List Price:  B$790.25
You save:  B$225.80
R1,825.21
List Price:  R2,555.37
You save:  R730.15
Лв180.14
List Price:  Лв252.21
You save:  Лв72.06
₩138,584.75
List Price:  ₩194,024.19
You save:  ₩55,439.44
₪365.84
List Price:  ₪512.19
You save:  ₪146.35
₱5,843.06
List Price:  ₱8,180.52
You save:  ₱2,337.46
¥15,455.24
List Price:  ¥21,637.96
You save:  ¥6,182.71
MX$1,831.18
List Price:  MX$2,563.73
You save:  MX$732.54
QR364.64
List Price:  QR510.52
You save:  QR145.87
P1,361.94
List Price:  P1,906.78
You save:  P544.83
KSh13,073.69
List Price:  KSh18,303.69
You save:  KSh5,230
E£4,833.36
List Price:  E£6,766.90
You save:  E£1,933.54
ብር5,779.16
List Price:  ብር8,091.05
You save:  ብር2,311.89
Kz87,254.87
List Price:  Kz122,160.31
You save:  Kz34,905.44
CLP$94,704.52
List Price:  CLP$132,590.12
You save:  CLP$37,885.60
CN¥725.11
List Price:  CN¥1,015.19
You save:  CN¥290.07
RD$5,921.37
List Price:  RD$8,290.16
You save:  RD$2,368.78
DA13,436.79
List Price:  DA18,812.04
You save:  DA5,375.25
FJ$225.82
List Price:  FJ$316.16
You save:  FJ$90.33
Q775.39
List Price:  Q1,085.58
You save:  Q310.18
GY$20,916.55
List Price:  GY$29,284.01
You save:  GY$8,367.45
ISK kr13,788.62
List Price:  ISK kr19,304.62
You save:  ISK kr5,516
DH983.22
List Price:  DH1,376.54
You save:  DH393.32
L1,774.76
List Price:  L2,484.74
You save:  L709.97
ден5,676.16
List Price:  ден7,946.86
You save:  ден2,270.69
MOP$803.68
List Price:  MOP$1,125.18
You save:  MOP$321.50
N$1,847.94
List Price:  N$2,587.19
You save:  N$739.25
C$3,679.41
List Price:  C$5,151.32
You save:  C$1,471.91
रु13,388.03
List Price:  रु18,743.78
You save:  रु5,355.75
S/374.11
List Price:  S/523.77
You save:  S/149.65
K386.64
List Price:  K541.31
You save:  K154.67
SAR375.13
List Price:  SAR525.19
You save:  SAR150.06
ZK2,610.12
List Price:  ZK3,654.27
You save:  ZK1,044.15
L457.84
List Price:  L641
You save:  L183.15
Kč2,336.96
List Price:  Kč3,271.83
You save:  Kč934.87
Ft35,994.10
List Price:  Ft50,393.18
You save:  Ft14,399.08
SEK kr1,079.53
List Price:  SEK kr1,511.39
You save:  SEK kr431.85
ARS$92,940.28
List Price:  ARS$130,120.11
You save:  ARS$37,179.83
Bs690.73
List Price:  Bs967.06
You save:  Bs276.32
COP$404,134.11
List Price:  COP$565,803.93
You save:  COP$161,669.81
₡52,857.56
List Price:  ₡74,002.70
You save:  ₡21,145.14
L2,475.36
List Price:  L3,465.60
You save:  L990.24
₲758,175.48
List Price:  ₲1,061,476.01
You save:  ₲303,300.52
$U4,025.98
List Price:  $U5,636.54
You save:  $U1,610.55
zł393.60
List Price:  zł551.06
You save:  zł157.45
Already have an account? Log In

Transcript

The first thing to do is of course, installing the pandas module. To do that simply open up the windows command line and type in pip install pandas. I have already installed this on my computer, however, you should go ahead and do it yourself. So we can be on the same page throughout the rest of this section. Of course, make sure you are connected to the internet before doing this. And now, assuming you paused the video and installed pandas, I'm going to open up a Jupiter notebook and we are going to see some basic operations with pandas.

Actually, I'm going to use the notebook from the previous lecture. And by the way, if you want to delete some cells, just click on a cell, hit escape, and then hit Double D. Again, click escape and DD and now we are left with this empty cell right here. As always, the first thing we should do is import the necessary module. So I'm going to do that right now. Import pandas. And I'm going to use Shift Enter To execute this line of code and move on to the next line.

Now, in order to work with data using pandas, we will always need to create a so called Data Frame, which is actually a Python object that holds the data that we are going to analyze. To be even more specific. a data frame is a two dimensional data structure with labelled axes or in simpler terms, imagine a data frame as being a table with rows and columns. And you're going to see this in practice shortly. Now let's create our first data frame. Let's call it for example d equals pandas dot data, frame, opening close parentheses.

And in between parentheses, we can specify a data source which can be for instance, a list of lists where each enter list is a row in the table be, again, the data source, but this time, we can specify the path to a text file, a CSV file, an Excel file, an SQL database table, and so on. And we will see that in the next video, and see various additional optional arguments to modify or enhance the functionality of the data frame. And again, you will see some examples very soon. For now let's consider a very basic example of a data frame. And let's pass a list of lists in between the parentheses and no additional arguments. Let's imagine we want to build and analyze a table of people showing their first name, age and occupation.

This means we should input a list where each element each in our list represents a person. So I'm going to paste in this line. So notice that we have a list of lists right here. And these are each of the inner list of course. separated by a comma, I'm going to enter a new line right here to also check out type of these. So I'm going to print type of D. And let's hit Shift Enter To execute these two lines of code.

And as you can see right here, we just created our first data frame. Okay, great. You can also use the DIR function on this new object to see all the available methods that pandas provides to manipulate and analyze the data inside a data frame. We will see some of them in action during this section of the course. For now, let's return to our data frame. And let's print it out to the screen to see the true power of Jupiter.

And also the way pandas just built a nice and clean table using the data we provided. So the Shift Enter. And now looking at this table right here, let's get our terminology right. You can see that pandas added these labels 012 and so on. To the rows and the columns of the table, remember that 01 and two are the column labels, these ones right here. And then 012, and three are the index labels.

This is how they are called index labels. Now you may say okay, but that looks kind of strange and ugly. And I would like to customize these labels in my data frame. Okay, I totally see your point. To do that, we would need to add an argument called columns, for which we need to pass a list where each element of that list is a column name in our table, the name we want to replace the default label with. So let me show you.

I'm going to move to this new cell right here. And I'm going to paste in the same data frame. Only this time we have an additional argument columns equals name, age and occupation, which are going to be the new names of our columns before execute Using this line of code, please keep in mind that the number of elements of the columns list must match the number of elements in each of the lists containing the data. Or in other words, when renaming the column labels, make sure that the number of elements in this list right here, the columns list matches the number of columns in your table. Now let's press Shift Enter. And let's see D once again.

Okay, great job this time instead of 01. And two, we have name, age, and occupation. What about the rows in the table? What if we want to change the index labels as well, in that case, we simply add a new argument in between the parentheses of the data frame, and this argument is called index, which also expects a list. Again, the number of elements in the index list must match the number of records in the table. So let's see this in action.

So I'm going to paste this in and notice that In addition to the data frame definition above, we have this index argument equals ID one ID to ID three and ID for a list of elements, a list of future index labels. Okay? Now let me hit Enter. And also, I'm going to type in D to see the table once again, Shift Enter. And indeed, this time, we have ID one, ID to ID three and Id four instead of 012, and three. Okay, it looks much better now, doesn't it?

At this point, we can use these labels these new labels as being the attributes of an object and called them whenever we want to extract a subset of information from the table. Let me show you what I mean by that. For instance, let's say d dot name, Shift Enter, and returns the name column from our data frame. So we have Andy, Jane, Robert and Maria. Let's also see d dot age Shift Enter And of course, this returns the age column from our data frame. And finally d dot occupation, Shift Enter, you guessed it returns the associated column.

Now, each of these data structures is called a series object when using pandas. So if we do a type of d dot age, Shift Enter, indeed, we have a series of objects, so please keep that in mind. Also, as I said earlier, we have lots of methods available for working with data frames and series objects. For instance, a basic example would be to find out the minimum and maximum values in a column. To do that, we need to use the correct series object and two specific methods called min and max. Let's see this.

So we have the dot h, dot min, open and close parentheses, Shift Enter, and we have 21, which is the lowest value in the age column. Similarly, we can use, for example, the dot name, dot Max, Shift Enter. And we get Robert as a result, since it is the largest value in the Name column, alphabetically speaking, what if we apply the min method to the entire data frame, for example? Well, in that case, we will get the minimum value for each of the columns in the table. Let's see this d dot mean, open and close parentheses, Shift Enter. Okay, so Indeed, we have Andy for the name 21 for the age and engineer for the occupation.

Okay, so this was a basic introduction to pandas. I hope you enjoyed it. Next, it's time to move away from this basic example where we use a list of lists to loading reading and analyzing data from various types of files, which is a real life use case for a lot of office jobs that require data analysis. You can find a link attached to this lecture pointing to the official documentation of pandas, where you can find all the available methods and operations that this amazing module provides. I'll see you in the next lecture.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.