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.


import_contacts

You may also like:

Related Posts

  • Python copy file to another directory
    17 January

    How to copy file to another directory in Python

    Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusion In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also work with paths […]

  • Check if variable is String in Python
    13 January

    Check if variable is String in python

    Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

  • Count the number of characters in a String in Python
    13 January

    Count number of characters in a string in python

    Table of ContentsWays to count the number of characters in a string in PythonUsing the len() functionUsing the for loopUsing the collections.Counter classConclusion In this post, we will see how to count number of characters in a String in Python. We can think of strings as a collection of characters, with every character at a […]

  • Copy file Python
    12 January

    How to Copy File in Python?

    Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

  • What is percent in python
    11 January

    What is % in Python?

    Table of ContentsWhat is % Operator in Python?The % Operator with strings in pythonThe % Operator as a placeholder for variables  in pythonThe % Operator as format specifiers in pythonConclusion In this article, we will cover what is % in Python and what are different ways to use % in Python. In python, there are […]

  • 09 January

    Convert set to String in Python

    Table of ContentsWays to convert set to string in PythonUsing the str() functionUsing the repr() functionUsing the join() functionUsing the map() functionConclusion Sets are an unordered collection of elements in Python. It does not contain duplicate elements and can be defined using the set() function in Python. Ways to convert set to string in Python […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.