How To Use apply() In Pandas (Python)

preview_player
Показать описание
↓ Code Available Below! ↓

This video shows how to apply functions to columns and rows pandas data frames using .apply(). The .apply() function operates on pandas series or data frames and applies a function to each element of a single series (such as each record in a column of a data frame) or to each row or column of a data frame. .apply() can be a useful way to generate aggregate statistics for each column or to generate new columns.

If you find this video useful, like, share and subscribe to support the channel!

Code used in this Python Code Clip:

import pandas as pd

data = pd.DataFrame({"power_level": [12000, 16000, 4000, 1500, 3000,
2000, 1600, 2000, 300],
"uniform color": ["orange", "blue", "black", "orange",
"purple", "green", "orange", "orange","orange"],
"species": ["saiyan","saiyan","saiyan","half saiyan",
"namak","human","human","human","human"]},
index = ["Goku","Vegeta", "Nappa","Gohan",
"Piccolo","Tien","Yamcha", "Krillin","Roshi"])

data

# Use .apply() to apply a function to a Series (single column)

def my_function(x, h, l):
if x > h:
return("high")
if x > l:
return("med")
return ("low")

data["power_level"].apply(my_function, args = [10000, 2000])

# Apply a function to each column with axis = 0
# Can be used to create new rows/summary rows

def mode(x):

# Apply a function to each row with axis = 1
# Can be used to create new columns/summary columns

def max_str_len(x):
return max([len(str(v)) for v in x])

# Apply a function to each row, referencing column names:

def make_char_string(x):

* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! I will use Unicode large < and > symbols in place of the standard sized ones. .

Рекомендации по теме
Комментарии
Автор

Never thought I would see anyone using Dragon Ball Z reference to explain a python function. Very helpful explanation. Thank you!

TheAkashchitnis
Автор

Just found your videos, keep up the great work! Thank you!!

ttrindademendes
Автор

i m still struggling to understand why the apply() function perform row wise operation at axis whereas other functions in pandas axis = 1 is column-wise..

ChungXlan
Автор

Hello Can you please help in using the apply() for the following: if one wants to increase the power_level by 2000 for the uniforms which have orange. ie how can we modify the entries of one column based on a condition on entries of another column by writing user defined functions and using apply(). I'm kind of struggling a bit on this kind of problem.

aksrivastava
Автор

thanks for putting the code in the discription. it's very helpful

lahirulowe
Автор

You do such a good job of explaining these concepts. Thank you!

philasoma
Автор

Can you use another column as an argument? Sometimes I need to calculate a result based on multiple columns?

michaelmolter
Автор

great examples and explanation, thank you

behradio
Автор

I still struggle to understand applications where I would use apply over just taking advantage of regular function broadcasting. I guess it only makes sense if you repeatedly use a similar function, and that function is complex.

michaelmolter
Автор

Thank you, really helpful
But plz use correct order of power level, gohan and Roshi have higher power level .😅

narendrasingodia
Автор

Excellent thank you so much. It's much more intuitive explaining things with dbz references. THANKS SUSCRIBING TO YOUT CHANNEL

ManoloTiburcio