Adding, Updating, Deleting Table Rows and Columns

Python 3: Automating Your Job Tasks Superhero Level: Automate Data Analysis Tasks with Python 3
14 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
€93.41
List Price:  €130.79
You save:  €37.37
£79.90
List Price:  £111.86
You save:  £31.96
CA$137.16
List Price:  CA$192.03
You save:  CA$54.87
A$153.08
List Price:  A$214.33
You save:  A$61.24
S$135.90
List Price:  S$190.27
You save:  S$54.36
HK$781.66
List Price:  HK$1,094.36
You save:  HK$312.69
CHF 91.23
List Price:  CHF 127.73
You save:  CHF 36.49
NOK kr1,107.76
List Price:  NOK kr1,550.92
You save:  NOK kr443.15
DKK kr696.86
List Price:  DKK kr975.63
You save:  DKK kr278.77
NZ$168.60
List Price:  NZ$236.05
You save:  NZ$67.44
د.إ367.24
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,972.60
List Price:  ৳15,362.09
You save:  ৳4,389.48
₹8,346.11
List Price:  ₹11,684.89
You save:  ₹3,338.77
RM475.54
List Price:  RM665.78
You save:  RM190.23
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,846.85
List Price:  ₨38,986.71
You save:  ₨11,139.85
฿3,694.58
List Price:  ฿5,172.56
You save:  ฿1,477.98
₺3,236.41
List Price:  ₺4,531.11
You save:  ₺1,294.69
B$519.35
List Price:  B$727.11
You save:  B$207.76
R1,861
List Price:  R2,605.48
You save:  R744.47
Лв182.73
List Price:  Лв255.83
You save:  Лв73.10
₩137,535.16
List Price:  ₩192,554.72
You save:  ₩55,019.56
₪374.71
List Price:  ₪524.61
You save:  ₪149.90
₱5,763.02
List Price:  ₱8,068.46
You save:  ₱2,305.43
¥15,525.12
List Price:  ¥21,735.79
You save:  ¥6,210.67
MX$1,695.96
List Price:  MX$2,374.42
You save:  MX$678.45
QR364.20
List Price:  QR509.89
You save:  QR145.69
P1,420.18
List Price:  P1,988.32
You save:  P568.13
KSh13,372.99
List Price:  KSh18,722.73
You save:  KSh5,349.73
E£4,798.26
List Price:  E£6,717.75
You save:  E£1,919.49
ብር5,719.92
List Price:  ብር8,008.12
You save:  ብር2,288.20
Kz83,641.63
List Price:  Kz117,101.63
You save:  Kz33,460
CLP$96,028.39
List Price:  CLP$134,443.59
You save:  CLP$38,415.20
CN¥723.89
List Price:  CN¥1,013.48
You save:  CN¥289.58
RD$5,819.58
List Price:  RD$8,147.64
You save:  RD$2,328.06
DA13,455.35
List Price:  DA18,838.03
You save:  DA5,382.68
FJ$226.17
List Price:  FJ$316.65
You save:  FJ$90.48
Q777.12
List Price:  Q1,088.01
You save:  Q310.88
GY$20,929.88
List Price:  GY$29,302.67
You save:  GY$8,372.79
ISK kr14,022.59
List Price:  ISK kr19,632.19
You save:  ISK kr5,609.60
DH1,011.06
List Price:  DH1,415.53
You save:  DH404.46
L1,768.33
List Price:  L2,475.73
You save:  L707.40
ден5,750.59
List Price:  ден8,051.06
You save:  ден2,300.46
MOP$805.69
List Price:  MOP$1,128
You save:  MOP$322.31
N$1,863.42
List Price:  N$2,608.86
You save:  N$745.44
C$3,672.13
List Price:  C$5,141.13
You save:  C$1,469
रु13,425.24
List Price:  रु18,795.88
You save:  रु5,370.63
S/376.35
List Price:  S/526.91
You save:  S/150.55
K385.89
List Price:  K540.26
You save:  K154.37
SAR375.01
List Price:  SAR525.03
You save:  SAR150.02
ZK2,677.10
List Price:  ZK3,748.04
You save:  ZK1,070.94
L464.84
List Price:  L650.79
You save:  L185.95
Kč2,347.91
List Price:  Kč3,287.17
You save:  Kč939.26
Ft36,370.18
List Price:  Ft50,919.71
You save:  Ft14,549.52
SEK kr1,092.76
List Price:  SEK kr1,529.91
You save:  SEK kr437.15
ARS$87,614.14
List Price:  ARS$122,663.30
You save:  ARS$35,049.16
Bs690.85
List Price:  Bs967.22
You save:  Bs276.36
COP$390,329.27
List Price:  COP$546,476.60
You save:  COP$156,147.32
₡51,017.42
List Price:  ₡71,426.43
You save:  ₡20,409.01
L2,476.75
List Price:  L3,467.55
You save:  L990.79
₲749,236.02
List Price:  ₲1,048,960.40
You save:  ₲299,724.38
$U3,833.15
List Price:  $U5,366.56
You save:  $U1,533.41
zł404.64
List Price:  zł566.52
You save:  zł161.87
Already have an account? Log In

Transcript

Hi, and welcome back. In this lecture, you're going to learn how to add, update and delete table rows and columns. From a table from a data frame. I have created a new Jupiter notebook. I have imported the pandas module and loaded the same JSON file as we did earlier. And first let's start with adding a column to a data frame.

For this, the JSON in between square brackets and in between double quotes, you enter the name of the new column, so batch ID, let's say equals and now you should enter a list where each element will be a value for each of the rows in the table. So we are creating the new column and we are also populating the column for each employee in the table. let me paste this in. I have added some sample batch IDs. Also. Let's see the JSON once again, Shift Enter Enter.

And as you can see, we have a new column added at the end of the table with a value for each of the employees. Now what if you want to add a new row or multiple rows to a data frame? For this, you will use the append method. And I'm going to paste this in as well. So this is the syntax, we have the JSON equals the JSON dot append. So we are updating the table in place basically, and in between the parentheses of the append method.

The first argument will be a list of dictionaries, where each dictionary is a row in the table, and each key value pair in the dictionary is made up of the column name and the value associated to that column for that particular row. And in my case, I am adding three new rows to the table. That's why I have three dictionaries inside this list. The second argument is this ignore index equals true which by default, set to false. This basically ignores the default indexing system of the table in case you have other indexes set up for the table. Okay, let's do a shift, Enter.

And now let's see our table once again. And as you can see, we have these three new rows being added to the table. Okay, now let's see how to update rows and columns in the table, thus modifying the data frame in place. First, let's start with updating a column. So let's say that for the badge ID column, instead of these numbers right here, we want a combination between the first name of the employee plus the year, let's say 2019. Okay, in order to do that, the Jason the name of the column to update so badge ID equals and now let's combine the first name and the year.

First of all, DJ son of first name plus a string Let's say 2019. Okay, Shift Enter. Let's see the table. And as you can see the badge IDs have been modified to the first name of each employee plus the year. Okay, great. Now what if you want to update a certain column in your table and insert the same value for all the rows in the table?

Let's say that, for instance, we want all the first names of our employees to be set to jack. Of course, this is not the best example. But in certain use cases, this can be pretty useful. So let's try this. The Jason first name, the name of the column to update equals, and now I'm going to use a trick and I'm going to explain it right away. So the JSON that shape of index zero times the new value to be inserted.

Let's say jack, shift, enter. Let's see are they Now, and indeed, the first name of each of our employees has been updated to jack. Why did I use this DJ some shape, because I wanted to get the number of rows in the table that needs to be updated. And let's remember that DJ son that shape returns the number of rows and columns in the table. And if we use an index, for instance, the JSON dot shape of zero, which is the forest value inside the parentheses right here, this will return the number of rows. Of course, this is pretty useful when you have a pretty large data set with maybe hundreds or thousands of rows, and you want to make sure that all the rows get updated.

Next, let's see how to update multiple rows based on a condition. Let's say that for each employee in the IT department, we want the salary to be set to $90,000 In order to do that, we have the JSON dot lock. And in between the square brackets of lock, we enter the condition. So the Jason of department equals equals ID, comma. Now we enter the column to be updated salary, and finally equals, as I said, 90,000. And now let's also print out the table.

And as we can see, all the employees in the IT department now have a greater salary, which is kind of cool. Okay, what about being more granular and using multiple conditions? So let's say that for all the employees in the IT department, who have networking skills, we want the salary to be updated to $100,000. So that would be only this employee right here, the lucky one. Let's see how to do that. I'm going to copy this and updated in this new cell.

So the first condition remains the same. Let's include it inside parentheses. And now let's use the AND operator and enter the second condition, which is the J, son of skills, equals equals networking. And the salary, as I said, will be updated to 100,000. Let's also print out the table, Shift Enter. And as you can see, the salary for this employee has been updated.

Okay, pretty cool. Now, what if we want to update multiple fields on the same row? Well, it's pretty simple. Let me show you. Let's say that for the employee whose ID is nine, so this would be jack Drake right here. We want to update the salary to $80,000 and also the phone number to another value.

So let's see how to do this. We have the JSON dot lock. And in between its square brackets, we enter First, the conditions. So the JSON of ID equals equals nine, comma. And then we have a list as the second argument, where each element is a column that we want to update the value for. So first of all, salary, comma, and phone.

Now we have equals, and now we have yet another list containing the updated values. So for the salary, let's say 80, thousands, comma and for the phone number, let's say all sevens. And also let's print out the table to the screen. Shift Enter. And if we look at jack Drake right here, we can see the updated phone number and salary. Great.

Okay, now let's see how to do it. rows and columns from a table from a data frame. And let's start by deleting a row by its label. And first, let's, for example, set the JSON dot set index of badge ID in place equals true, in order to update this table, the indexing of this table, Shift Enter. Now let's see the JSON. Okay, and now let's delete a row by its label.

And let's see how to do that the JSON dot drop, and in between the parentheses of drop, we should enter the label let's say look 2019. So the first row in the table, look 2019 Shift Enter. And you can see that this employee this row is now gone from the table. Now if by any chance you have a row label with the same name, as a column in your table, Then you can explicitly specify that you want to delete the row with that label and other column with the same label by passing zero or index as an argument. So instead of the JSON drop of Luke 2019, I could have used an additional argument, either 02 specify that this is a row, or index in between quotes or double quotes, again to specify that this is a row. Of course now if we check the JSON, once again, we can see that look is still in the table.

That's because our change was not made in place. In order to make this change in place, all you have to do is enter in place equals true and then the row labeled as look 2019 will be permanently deleted from the table. Okay, now moving forward, what if we want to explicitly delete column by using its label. Well, that's pretty simple as well, the JSON dot drop. And let's say that we want to drop the phone column from the table. In this case, in between double quotes, we enter the name of the column to drop phone with a capital P, comma, and we can specify either one, or columns as the second argument to specify that this is indeed a column.

And of course, if you want to make this modification in place, then you should also add in place equals true, but in our case, I don't want that. So let me run this as it is, Shift Enter. And you can notice that the phone column is now gone from the table. Great. You can also delete rows and columns by indexes. And let me show you how to do that.

For instance, looking at our table, let's say that we want to delete the row with index Number four, that will be counting from 00123 and four, so we want to delete Emily 2019 from the table. In order to do that, the JSON dot drop, and in between the parentheses of drop, we just enter the JSON dot index, and in between square brackets, the index for Shift Enter. And now we can see that Emily is now gone from the table. Also, you can use a slice to delete multiple rows at the same time. For instance, let's say that we want to delete the first four rows in the table. So these four rows right here.

To do that, the JSON dot drop in between parentheses we have the JSON dot index, and in between square brackets, we have zero up to but not including index four. So that would be index 012, and three, the first four rows in the table. Let's see test this shift enter. And indeed, we have these four rows removed from the table. However, you can see that we have Emily 2019 back into this table because we haven't made the modification in place when we deleted Emily from the table up here. Similarly, let's see how to delete columns by indexes.

So let's say that we want to delete the department column, that would be the second column of the table, the one at index one. So let's see the JSON dot drop of the JSON dot columns. And let's specify the index that will be index number one, Shift Enter. Sorry, I forgot an argument here. So we should enter after the comma one to specify that this is a column, shift, enter again. And indeed, we have the department column, gone from the table.

And now as we did with room We can delete multiple columns at the same time by using a slice. So let's see the Jason at this moment, Shift Enter. And let's say that we want to delete the last three columns in the table, that would be columns at indexes. This is 0123456, and seven. So we should use the JSON dot drop. In between the parentheses we have the JSON dot columns, and all the slides starting at index five, colon all the way to the end of the table.

And now let's enter a comma and enter one because we are referring to columns, Shift Enter. Okay, I mistyped the method is columns not column. Now let's run it once again. And indeed, the last three columns in the table have been removed. Okay, so that's about it with all the operations and methods that the panda's module provides for dealing with data and data analysis. Of course, there are many, many others.

But for now, I think we've covered quite a lot. And in the next lecture, we are going to build an application using the concepts and skills that you've learned throughout this section. I'll see you in the next video.

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.