Welcome back.
In the last lecture we looked at a few different ways to manipulate data especially missing data.
We looked at in place.
So you want to perform an operation on Panda's data frames in place you can set the pace parameters
to true or you can reassign it by using.
We don't have a reassignment example where you can reassignment by setting it to equaling something.
Now we've had enough of working with missing data.
How do we create data from existing data or pandas has a few different ways that you can create data
such as building a new column.
Let's have a look at a couple of different ways.
Let's start with a series so communist first.
We'll go a column from series then we'll create a series called seats column equals Payday series and
maybe inside that series we'll put a few numbers of five because a few of our cars have five seats or
actually all of our cars have five seats but we're not going to fill it out.
So at car sales seats another way to make a new column.
We'll a comment here new column called seat remember our car sales data frame is the one we were working
with above.
So we can view it here.
So car sales new column.
Now it's very similar to how you would select a column to create a new column.
But remember we're going to run the code first before we discuss it.
So we're going to set it equal to the seats column which is that series we've just created and now we
want to view our car sales data from excellent.
So we'll see what this is done.
We've got the exact same columns as before but this time we've now got seats on the right hand side.
So that's created a column on the very right.
When you create a new column in panders by default it appears on the very right hand side of your data
frame but we've got some missing values here.
Okay.
That's because our series was only five by five so it's length five with the value five I'm getting
confused all these fives here.
So this is just to say this Toyota white car 400 thousand dollars because we haven't fixed up that price
column and it has five seats.
But how would we fill this in and maybe we can use car sales with the seats column we saw this before
in the previous video fill in a we want five.
There we go.
All we want in place.
Yeah.
So we don't have to implies equals true shift.
Now we have a look at car sales.
Mistake.
There we go.
All of our cars have five seats.
Beautiful.
We kind of already knew that already.
Well maybe some didn't have five seats.
Now there's another way to create a column and that's from a point in the list which is similar to our
series column from list might type in Python at least so we won't maybe fuel economy that's something
else you want to consider when you buy a car these days you want to make sure that you're getting the
best value or the best bang for your buck.
Did I spell economy.
No.
Mix them up.
There we go.
We want maybe seven point five litres per 100 kilometres some nine point two 5.0 nine point six.
We're just making these numbers up here.
There we go.
It's about five long as well.
We want car sales what should we call this column fuel per hundred came.
That's nice and easy to remember.
Equals.
We're going to set it to being equal to our fuel economy list economy and then we're going to have a
look at it here value era.
What do we get wrong length of value does not match length of index Huh says an interesting era.
So maybe we need more values in here.
Let's keep filling it up.
Eight point seven three.
This is a really fuel efficient car.
See what happens.
Still not the same.
What do we need.
Zero.
One two three four five six seven eight mumble should do four or five.
There we go.
So if you want to create it from a list did that on purpose wink wink it has to be the same length as
your existing data frame whereas a series can be different kind of length to what your data frame already
is.
So we needed nine or 10 values in here because it starts from zero because our original car sales data
frame was of length 10 all right.
Let's have another look at the way.
What if we wanted to create a column from another column.
So as if they wanted to figure out how much should this car has fuel it's used in its lifetime.
So if this is per 100 kilometers it's done a hundred and fifty thousand kilometers.
Maybe if we divided the odometer column by one hundred and times it by this column we could work out
how many litres of fuel this car is used in its whole entire life.
Let's try that out.
So if we go car sales door.
What's a good name.
Total fuel used beautiful equals car sales.
We want the motor column car sales odometer maybe we press tab area tab auto complete.
Now we want to divide this by a hundred because this is fuel per 100 kilometers and so we could times
that by car sales.
We want fuel her tab per tab.
No I press tab to get fuel per 100 km.
Now what do you think this will do this is gonna create a new column because this string doesn't exist
in our data frame that's going to take the odometer column another beautiful thing about Pan is numeric
columns is that you can perform operations directly on them and then it's going to divide the odometer
column by one hundred and times it by the fuel per 100 km column let's see what happens we want to view
our data from beautiful so this is gonna be total fuel used in liters or maybe we could have known that
better if we create this new column by typing in liters l it's gonna create another column total fuel
used l That's right we don't mind that for now so 11 thousand two hundred eighty three ladies if you
did that and one guy that'd be quite expensive depending on the price of fuel where you live right now
is one more easier way to create a column we can do it from create a column from a single value let's
do that.
Car sales maybe we want a simple one.
Number of wheels equals 4 because if your car didn't have four wheels you really wouldn't be out of
drive now and you can start to see how our data frame can evolve over time Ryan we have manipulated
a few things.
Maybe some of these columns aren't very useful in the long run but it's just an example of how you can
quickly use panders to change up your data frame.
We've adjusted the price column with lowered the make column we've added a bunch of our own columns
is a step you'll take in a data size or machine learning project right.
Maybe we'll add one more column.
We want to know if our cars pass to road safety and by default it equals true.
This is just to give another example of different types of columns D types.
So yeah there we go.
We've got a bull column on the end now so bull.
True or false.
We've got a few ints we've got a few floats we've got some object columns we've got true.
So this is how versatile panders is right you can use many different data types within and now what
if we wanted to remove one of our columns like this one because it's got the L at the top.
That's less confusing than this.
We don't know what measurement that's in.
You can use the function drop it's going to drop a drop column so we'll go total if you will use.
That's our column name.
Now we need to give it drop requires the parameter axis.
So axis equals one if you remember the anatomy of our data frame.
Let's see if we have it here.
That's because columns are on axis equals 1.
Remember how I said some functions require access.
It took me a while to remember this but that's what you have to remember is if you're talking about
a column you're talking about access 1.
Let's do that drop.
Beautiful car sales.
It's still got that we keep forgetting to re-emphasize this point.
We need to either reassign or use in place method.
So we're reassigning our car sales data frame here drop car sales.
Now we've dropped in beautiful where that data frame.
That's amazing.
All right.
So we've seen out to create a few different columns here.
We've seen how to drop a column if we don't want it.
There's a few more things we want to do for manipulating our data frames.
But before this lecture gets too long again go back.
I want you to if you're going to take a little break take a little break.
But otherwise before the next election try creating a column of your own.
Something can be as simple as you like but just practice typing out some code like this.
Maybe you do a little operation can be as fancy or as UN fancy as you like but just practice making
a new column of your own.
And I'll see you back in the next video.