PYTHON Pandas Function Implementation

Furkan Gulsen
Analytics Vidhya
Published in
2 min readOct 12, 2020

--

You should be aware of three important ways to apply functions of your own or another library to pandas objects. The appropriate method to use depends on whether your function is waiting to run on the entire DataFrame, row, or column-wise or element-wise.

  • Table Function Implementation: pipe ()
  • Row or Column Function Implementation: apply ()
  • Element Function Implementation: applymap ()

Table Function Implementation

Custom operations can be performed by passing the appropriate number of parameters as function and pipe arguments. Thus, the processing is performed on all data. For example, add 2 as a value to all items in the Dataframe.

Now we will use the special function to operate on the DataFrame.

Let’s see the full schedule:

   col1      col2       col3
0 3.444891 3.390373 1.838314
1 1.116019 2.015870 1.563431
2 3.615770 1.656108 1.313535
3 2.217141 -1.208859 0.698390
4 2.975013 -0.120729 3.568476

Row or Column Function Implementation

Arbitrary functions can be applied along the axes of a data frame or panel using the apply () method, which takes an optional axis argument, like descriptive statistics methods. By default, the process performs column-wise, taking each column as an array-like.

Example 1:

col1    -0.351974
col2 -0.495009
col3 0.111530
dtype: float64

Example 2:

col1    0.433517
col2 2.410229
col3 1.663377
dtype: float64

Example 3:

0   -0.923194
1 -0.584883
2 0.309061
3 0.217995
4 -0.873165
dtype: float64

We converted the table to a row structure by passing an axis parameter.

Element Function Implementation

Not all functions can be vectorized (neither another array nor NumPy arrays returning any value), similar to the applymap () methods in dataframe and the series map () accepts any Python function that takes a single value and returns a single value.

Example 1:

0    110.724381
1 -117.182626
2 -144.942798
3 17.917625
4 70.566406
Name: col1, dtype: float64

Example 2:

   col1       col2       col3
0 344.4891 339.0373 183.8314
1 111.6019 201.5870 156.3431
2 361.5770 165.6108 131.3535
3 221.7141 -120.8859 69.8390
4 297.5013 -12.0729 356.8476

--

--

Furkan Gulsen
Analytics Vidhya

🧑‍💻 As a software engineer, I write about software, artificial intelligence, productivity, and everything I know