A tip A day – Python Tip #3 – Pandas Apply Function

Apply function takes a function as an argument and execute the function in all the elements of the dataframe.

For example, if we want to create a new column which is the square root of another column’s values or apply a complex function and combine one or more columns or when creating new features using the existing features for feature engineering.

Syntax: df.apply(func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds)

df – a pandas series.

func – A lambda function or a normal function

axis – 0 – rows which is default, 1 – columns

Lets try this using an example.

Output:

orangeapples
030
123
207
312

For example, we need to create a new series by taking square root of another column. The below code does this.

Output:

Apply function simplifies this.

Output:

orangeapplesorange_sqrt
0301.732051
1231.414214
2070.000000
3121.000000

We have done the same functionality with less code.

Using Lambda function:

Out[25]:

orangeapplesorange_sqrtorange_sq
0301.7320519
1231.4142144
2070.0000000
3121.0000001

Today we learned about Apply function.

Hope you are excited to practice what we have learned now.

We will meet with a new tip in Python. Thank you! 👍

Like to support? Just click the heart icon ❤️.

Happy Programming!🎈

0

Leave a Reply

Your email address will not be published. Required fields are marked *