Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

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

Thepipe() 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 thepipe() function.

import pandas as pdimport numpy as npdef adder(ele1,ele2):   return ele1+ele2df = 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)

Itsoutput is as follows −

Original DataFrame:        col1      col2      col30  2.349190  1.908931 -0.1214441  1.306488 -0.946431  0.3089262 -0.235694 -0.720602  1.0896143  0.960508 -1.273928  0.9430444 -1.180202 -0.959529  0.464541Modified DataFrame:        col1      col2      col30  2.349190  1.908931 -0.1214441  1.306488 -0.946431  0.3089262 -0.235694 -0.720602  1.0896143  0.960508 -1.273928  0.9430444 -1.180202 -0.959529  0.464541

Row or Column Wise Function Application

Theapply() 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 thenp.mean() function calculates the mean of each column.

import pandas as pdimport numpy as npdf = 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)

Itsoutput is as follows −

Original DataFrame:        col1      col2      col30 -0.024666  0.058480  0.6585201 -0.040997  1.253245 -1.2423942  1.073832 -1.039897  0.8406983  0.248157 -1.985475  0.3107674 -0.973393 -1.002330 -0.890125Result: col1    0.056587col2   -0.543195col3   -0.064507dtype: float64

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

Example: Applying a Function Row-wise

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

import pandas as pdimport numpy as npdf = 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)

Itsoutput is as follows −

Original DataFrame:        col1      col2      col30  0.069495 -1.228534 -1.4317961  0.468724  0.497217 -0.2701032 -0.754304  0.053360 -1.2983963  0.762669 -2.181029 -2.0677564  0.129679  0.131104  1.010851Result: 0   -0.8636121    0.2319462   -0.6664463   -1.1620394    0.423878dtype: float64

Example: Applying a Lambda Function

The following example applies thelambda function to the DataFrame elements using theapply() method.

import pandas as pdimport numpy as npdf = 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)

Itsoutput is as follows −

Original DataFrame:        col1      col2      col30 -1.143522  0.413272  0.6338811  0.200806 -0.050024  0.1085802 -2.147704 -0.400682 -1.1914693  2.342222 -2.398639  0.0631514 -1.071437  1.895879 -0.916805Result: col1    4.489926col2    4.294518col3    1.825350dtype: float64

Element Wise Function Application

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

Example: Using map() Function

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

import pandas as pdimport numpy as npdf = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])# My custom functiondf['col1'].map(lambda x:x*100)print(df.apply(np.mean))

Itsoutput is as follows −

col1    0.480742col2    0.454185col3    0.266563dtype: float64
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp