Pandas apply function to column

Pandas apply function to column

We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over a specific column or the whole table of the stored data.

This tutorial demonstrates the different methods available to apply a function to a column of a pandas dataframe in Python.

How do I apply function to column in pandas?

Here are multiple ways to apply function to column in Pandas.

Using dataframe.apply() function

The dataframe.apply() function is simply utilized to apply any specified function across an axis on the given pandas DataFrame.

The syntax for the dataframe.apply() function is:

The dataframe.apply() takes in a couple of parameters, all of which are mentioned below:

  • func: It specifies the function that needs to be applied.
  • axis: It specifies the axis along with which the function needs to be implemented. The value 0 denotes column while 1 denotes row. By default, its value is taken as 0.

These two parameters are essential in order to understand the functioning and implementation of this method. Further information on the other optional parameters that the function takes in can be accessed here.

The following code uses the dataframe.apply() function to apply a function to a specific column in pandas

The above code provides the following output:

X Y Z
0 3 5 3
1 4 6 4
2 5 7 5
Explanation:
  • The numpy and pandas libraries are imported to the code first.
  • A pandas DataFrame named dfa is created and is initialized.
  • A function that is to be implemented to the column of the DataFrame is created.
  • The function is then implemented to the second column Y of the given DataFrame using the apply() function.

Using lambda function along with the apply() function

A lambda function is an unnamed function that represents only a single expression while taking any number of arguments in it.

The lambda function can be tweaked into the apply() function to apply a function to a specific column. This method shortens the length of the code as compared to the method above.

The following code uses the lambda function along with the apply() function.

The above code provides the following output:

X Y Z
0 3 5 3
1 4 6 4
2 5 7 5

The working of this method is similar to the simple dataframe.apply() method mentioned above, with the only difference being that we do not have to specifically create a function and can simply use a lambda function directly in the apply() function.

Using dataframe.transform() function

The dataframe.transform() function is utilized in calling a given function func on self and creating a DataFrame that contains all the transformed values, provided the length of the transformed DataFrame is the same as that of the initial value.

The syntax for the dataframe.transform() function is:

Similar to the apply() function, the transform() function contains several parameters:

  • func: It specifies the function that needs to be applied.
  • axis: It specifies the axis along with which the function needs to be implemented. The value 0 denotes column while 1 denotes row. By default, its value is taken as 0.

The dataframe.transform() takes in several other parameters which do not need to be explained to implement this method simply. However, more details on all the parameters of the transform() function can be found here.

The following code uses the dataframe.transform() function to apply a function to a specific column in pandas.

The above code provides the following output:

X Y Z
0 3 5 3
1 4 6 4
2 5 7 5
Explanation:
  • The numpy and pandas libraries are imported to the code first.
  • A pandas DataFrame named dfa is created and is initialized.
  • A function that is to be implemented to the column of the DataFrame is created.
  • The function is then implemented to the second column Y of the given DataFrame using the dataframe.transform() function.

Using map() function

The map() function which is provided by Python is utilized to seek a particular function to all the elements in any given iterable. It returns the iterator itself as the result.

The map() function can be utilized in place of the apply function.

The following code uses the map() function to apply a function to a specific column in pandas.

The above code provides the following output:

X Y Z
0 3 5 3
1 4 6 4
2 5 7 5
Explanation:
  • The numpy and pandas libraries are imported to the code first.
  • A pandas DataFrame named dfa is created and is initialized.
  • A lambda function is utilized in this case to specify the changes
  • This lambda function is then implemented to the second column Y of the given DataFrame using the map() function.

Using NumPy.square() function

NumPy is an abbreviation for Numerical Python and is a library that Python provides which is utilized in dealing with and manipulating arrays and tabular data. The numpy.square() function is a simple mathematical function that returns another array with the selected column values as the square of the original values.

The following code uses the numpy.square() function to apply a function to a specific column in pandas.

The above code provides the following output:

X Y Z
0 3 9 3
1 4 16 4
2 5 25 5
Explanation:
  • The numpy and pandas libraries are imported to the code first.
  • A pandas DataFrame named dfa is created and is initialized.
  • The numpy.square() function is then implemented to the second column Y of the given DataFrame in Python.

We should note that thenumpy.square() function can only change the elements and implement a square value of the set of elements of a column and cannot apply any other function to it.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

  • 11 June

    Save Numpy Array as Image in Python

    Table of ContentsSave numpy array as image in PythonHow to save numpy array as image in Python?Using the PIL library to save numpy array as image in PythonUsing the matplotlib.image library to save numpy array as image in PythonUsing the opencv library to save numpy array as image in PythonUsing the imageio library to save […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.