How do I apply a function to a pandas Series or DataFrame?

pandas assign
This is a topic that many people are looking for. is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, would like to introduce to you How do I apply a function to a pandas Series or DataFrame?. Following along are instructions in the video below:

hello and welcome back to my Q&A video series about the pandas library in Python and the question for today comes from a YouTube commenter who asks about two different concepts number one loke ill oak and IX when to use each differences etc and then second apply map and apply map okay Ive already got a video about the first of these two which I will link to in the description below and thats a very important concept how to use loca specially but today were going to focus on the second apply map and apply map now before we get into the content I just want to let you know you should stick around to the end of this video for three important announcements okay so lets dig into the content as always we will import pandas as PD and we need a data set and were to use the training data set from kegels Titanic competition okay so HTTP fit dot Lee slash kaggle train and lets go ahead and look at the head okay so each record in this data set represents a passenger on the Titanic okay and were going to first go through the map method so theres map apply map and apply so were going to start with map map is a series method okay and heres what were going to use it for lets say that you need to create a dummy variable for sex and what that means is I want to translate sex which is male and female into 1 and 0 okay so were going to use map and map allows you to map an existing value of a series to a different set of values so what does that mean Ill show you what that how we do that were going to create a new column called sex num and so well say train dot sex map and were going to pass it a dictionary and were going to say female : 0 which means translate or map female 2 0 and map male 2 1 okay so well run that and then lets compare a sex and sex num okay so Im going to use the loke method dot lok and say I want to see row 0 through 4 and I want to see columns sex and sex num okay and what we see is that male has been translated to 1 and female has been translated to 0 now ah there is actually more you can do with the map method but what I have just shown you mapping values is what its best for and so thats actually all I use it for alright so were going to go ahead and move on to apply now apply is actually both a series method and a data frame method so were going to start with apply as a series method okay so what does

apply do it applies a function to each element in a series okay so lets see this in action lets pretend I want to calculate the length of each of these strings in the name column the length of each string and create a new column called name length that contains that integer value meaning how many characters are here and here and here okay so were going to use apply for this and again were gonna use it as a series method so were in our new column is going to be called name length and were going to say train dot name dot apply okay when okay so were applying the Len function pythons Len function which when applied to a string checks the length of that string okay so lets compare name and name length so use dot loka ghin so first four rows and we want name and name length okay and what we see is that weve now have the name length column this is twenty-three characters this is 51 characters this is 22 characters etc okay so the apply series sorry the apply method um when used as a series method applies this function to this series and outputs the result okay it applies it to every element in the series now notice you do not say Len with like parenthesis you just pass it the name of the function okay let me give you some more examples of applying so its actually relatively common to use apply with like a numpy function okay so for instance lets go ahead and import numpy as NP okay and heres one example Im going to say Im going to look at this the fare column and this is the fare and dollars or some currency and lets say I want to round it up okay so I want to round this to eight around this 270 to around this to eight etc okay Im going to use numpy ceiling function c e IL okay so heres how well do it im going to create a new column called fare c l equals train fare dot apply MP dot c l– okay so lets run that so I applied this function to this series and save the results here and again lets compare the two columns train doubt lok 0 colon 4 and I want to look at fair and fair seal okay so it was indeed rounded up okay and there you go alright now lets use apply to solve a harder problem okay lets extract the last name of each person into its own column so what do we need to do well we need to get the part before the comma alright so how are we going to do that you might think well we can probably do this with a string method if youve seen my video on string methods so um

were going to say train dot name dot stir meaning string dot split and were going to split on commas and Ill go ahead and put head okay so Im splitting name on commas alright now this looks very similar to what we saw up here uh Braund comma mr. Owen Harris bran come in mr. Owen Harris but theyre actually very different and this is very important okay so up there it was a string this has now split it into a list of strings so the series that is output is a low is a series of Python lists each list is made up of strings so in other words this is a list of length two and heres the first element comma heres the second element and actually includes a space right there okay so that comma is not like the comma in the string that is a comma separating list elements all right so weve successfully lit it but all we want is the first part so how do we just get the first part what we really need to do is say hey pandas I want you to take this result and I want to pull out the first list element from each series element so Im gonna actually write a function to do this and then were going to apply it okay so Im going to call it get element and you pass it a list called my list and a position okay and what happens its simple were going to say return my list bracket position so if I pass it a list and I say position zero it will return to me element zero from that list okay so what are we going to do now well were going to take this and then were going to say dot apply get element and and say position equals zero so Im saying pandas take this series apply this function to the result on every element and pass it keyword argument position equals zero so pass position equals zero to this function Ive created okay and well put dot hat on the end okay and that actually does it here is my series of strings that is just the last names of these people now you might be thinking um we could actually do this with a lambda function if youre familiar with lambda functions and so thats what Ill do Ill rewrite this you dont actually need to make your own function called get element for something so simple you can actually rewrite this as lambda function so lambda X colon X bracket 0 okay and thatll do the same thing if you are familiar with lambda functions this will be pretty clear and lambda functions are actually used a lot with apply methods okay so that is that is it with apply as a series method now lets move on to

apply as a data frame method okay so were for this were gonna use another data set and Im going to say drinks equals PD read CSV and vitaliy slash drinks by country okay and what were looking at is a data set of alcohol consumption by country okay so apply as a data frame method what does it do it applies a function along either axis of a data frame okay so Im going to actually use a subset of this data frame Im going to say drinks Lok I want all columns and Ill sorry all rows and I want the beer servings through wine servings columns okay so this is the data frame we are working with okay so what am I going to do if I do dot apply here Im using it as a data frame method not a series method Im applying it to the entire data frame so what Im going to say Im going to say apply max axis equals zero now what am I saying Im saying I want the apply method to travel on axis zero which is this direction the down direction okay and I want you to apply the max function Pythons max function okay so apply it in this direction okay and heres what results beer servings 376 spirit 438 wine 370 so it figured out the max value in each of those columns because it was operating over axis zero okay so ah now were going to change it to axis equals one okay and that will be I want the max value in each row because axis equals one goes this direction okay so now we see the max value in those three columns is 0-1 30 to 25 312 so those are the results we are seeing here okay and I will show you one other trick I use quite a bit which is um sometimes you dont care about the maximum value in a row but you want to know which column is the maximum this is where NP Arg max is super useful so I run that and this tells me that um well there is no which is largest for Afghanistan well any of them but it will default to the first so it says beer servings is the largest here spirit servings is the largest for the second beer servings is the largest for the third wine servings for the fourth etc so thats a neat little trick you can do with NP dark max okay all right finally lets get into apply map so we covered map which is a series method we covered apply as a series method and we covered apply as a data frame method so now lets get to apply map which is also a data frame method okay so um Im just going to show you one example here and what apply map

so let me change that apply map what apply map does is to apply a function to every element of a data frame okay every element of a data frame okay it doesnt go this direction or this direction it applies it to every element okay so for instance if I do apply map float it will change every element in this data frame to a floating point its actually starting out as an integer changes to a floating point okay now you can actually use this to overwrite the existing data frame columns okay so Ill just show you if if you just say what we want is just drinks dot Lok all this stuff equals during start lok the rest then we can do drinks ahead and we will see that now the data frame has changed from integers to floating point numbers and we have accomplished our objective in that case okay so theres no bonus for today but as I said I have three important announcements for you number one this is actually going to be the last video in the pandas Q&A series for now okay now why is that well each video takes me between four and eight hours to create and I have some other projects to focus on right now so Im not able to devote that time for the time being however I might be creating more videos in this series later on so please keep asking questions please post in comments I will be happy to interact with you there and keep answering your questions that way okay so announcement number two I have other videos you can watch so if you didnt know this is video number 30 in this series so theres 29 other videos I have a video series called introduction to machine learning with scikit-learn thats almost seven hours long I have a very popular series called introduction to get and github and I have some other videos I will link to all of this in the description below this video okay and finally third announcement if you didnt know I teach online courses in data science and machine with Python okay if you liked this video series you will love my courses so what do I want you to do I would encourage you to sign up for my email newsletter right now okay theres a link in the description below for how to sign up what do you get by signing up number one youll get priority access to my future courses number two youll get access to exclusive content I dont share publicly and a lot of other stuff okay so please do sign up for my newsletter um that is it so thank you so much for joining me for this entire series it has been a real pleasure teaching you so take care and I hope to see you again soon

python, pandas, data science, data analysis, tutorial
Thank you for watching all the articles on the topic How do I apply a function to a pandas Series or DataFrame?. All shares of are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.

Leave a Comment