Python Pandas - Function Application



Pandas provides powerful methods to apply custom or library functions to DataFrame and Series objects. Depending on whether you want to apply a function to the entire DataFrame, row- or column-wise, or element-wise, Pandas offers several methods to achieve these tasks.

In this tutorial, we will explore three essential methods for function application in Pandas −

  • Table wise Function Application: pipe()
  • Row or Column Wise Function Application: apply()
  • Element wise Function Application: map()

Let's dive into each method and see how they can be utilized effectively.

Table-wise Function Application

The pipe() function allows you to apply chainable functions that expect a DataFrame or Series as input. This method is useful for performing custom operations on the entire DataFrame in a clean and readable manner.

Example: Applying a Custom Function to the Entire DataFrame

Here is the example that demonstrates how you can add a value to all elements in the DataFrame using the pipe() function.

import pandas as pd
import numpy as np

def adder(ele1,ele2):
   return ele1+ele2

df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
print('Original DataFrame:\n', df)

df.pipe(adder,2)
print('Modified DataFrame:\n', df)

Its output is as follows −

Original DataFrame:
        col1      col2      col3
0  2.349190  1.908931 -0.121444
1  1.306488 -0.946431  0.308926
2 -0.235694 -0.720602  1.089614
3  0.960508 -1.273928  0.943044
4 -1.180202 -0.959529  0.464541

Modified DataFrame:
        col1      col2      col3
0  2.349190  1.908931 -0.121444
1  1.306488 -0.946431  0.308926
2 -0.235694 -0.720602  1.089614
3  0.960508 -1.273928  0.943044
4 -1.180202 -0.959529  0.464541

Row or Column Wise Function Application

The apply() function is versatile and allows you to apply a function along the axes of a DataFrame. By default, it applies the function column-wise, but you can specify row-wise application using the axis parameter.

Example: Applying a Function Column-wise

This example applies a function to the DataFrame columns. Here the np.mean() function calculates the mean of each column.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), columns=['col1', 'col2', 'col3'])
print('Original DataFrame:\n', df)

result = df.apply(np.mean)
print('Result:\n',result)

Its output is as follows −

Original DataFrame:
        col1      col2      col3
0 -0.024666  0.058480  0.658520
1 -0.040997  1.253245 -1.242394
2  1.073832 -1.039897  0.840698
3  0.248157 -1.985475  0.310767
4 -0.973393 -1.002330 -0.890125

Result:
 col1    0.056587
col2   -0.543195
col3   -0.064507
dtype: float64

By passing value 1 to the axis parameter, operations can be performed row wise.

Example: Applying a Function Row-wise

This function applies the np.mean() function to the rows of the pandas DataFrame.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), columns=['col1', 'col2', 'col3'])
print('Original DataFrame:\n', df)

result = df.apply(np.mean, axis=1)
print('Result:\n',result)

Its output is as follows −

Original DataFrame:
        col1      col2      col3
0  0.069495 -1.228534 -1.431796
1  0.468724  0.497217 -0.270103
2 -0.754304  0.053360 -1.298396
3  0.762669 -2.181029 -2.067756
4  0.129679  0.131104  1.010851

Result:
 0   -0.863612
1    0.231946
2   -0.666446
3   -1.162039
4    0.423878
dtype: float64  

Example: Applying a Lambda Function

The following example applies the lambda function to the DataFrame elements using the apply() method.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), columns=['col1', 'col2', 'col3'])
print('Original DataFrame:\n', df)

result = df.apply(lambda x: x.max() - x.min())
print('Result:\n',result)

Its output is as follows −

Original DataFrame:
        col1      col2      col3
0 -1.143522  0.413272  0.633881
1  0.200806 -0.050024  0.108580
2 -2.147704 -0.400682 -1.191469
3  2.342222 -2.398639  0.063151
4 -1.071437  1.895879 -0.916805

Result:
 col1    4.489926
col2    4.294518
col3    1.825350
dtype: float64

Element Wise Function Application

When you need to apply a function to each element individually, you can use map() function. These methods are particularly useful when the function cannot be vectorized.

Example: Using map() Function

The following example demonstrates how to use the map() function for applying a custom function to the elements of the DataFrame object.

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])

# My custom function
df['col1'].map(lambda x:x*100)
print(df.apply(np.mean))

Its output is as follows −

col1    0.480742
col2    0.454185
col3    0.266563
dtype: float64
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy