Transform pandas DataFrames using apply() and applymap() functions

Overview:

  • A function can be applied on a pandas DataFrame object to transform its elements. The pandas DataFrame is designed in such a way that each call to the applied function, can be passed of data: either row-wise, column-wise or element wise.
  • When the function applied on the DataFrame is designed to take a pandas series or an ndarray, the method to be used is DataFrame.apply().
  • On the other hand if the function to be applied on the DataFrame takes only one Python object as a parameter then the method to be used is DataFrame.applymap().

Applying a Python function to each row or column of a pandas DataFrame:

  • The example below sums the elements of a pandas DataFrame both row-wise as well column-wise using the numpy.sum() function.
  • The Python code uses the method apply(), which takes a function as parameter and applies the function on each column or each row of the DataFrame. Method apply() works on a pandas series or a ndarray.

Example:

# Example Python program to apply a function

# that transforms the elements of a pandas DataFrame

 

import pandas as pd

import math

import numpy as np

 

numericData = [(2, 4, 6),

               (8, 10, 12),

               (45, 50, 55)];

 

dataFrame = pd.DataFrame(data=numericData);

sum       = dataFrame.apply(np.sum);

 

print("DataFrame:");

print(dataFrame);

 

print("Sum of each DataFrame column:")

print(sum);

print(type(sum));

 

print("Sum of each DataFrame row:")

sum       = dataFrame.apply(np.sum, axis=1);

print(sum);

print(type(sum));

 

Output:

DataFrame:

    0   1   2

0   2   4   6

1   8  10  12

2  45  50  55

Sum of each DataFrame column:

0    55

1    64

2    73

dtype: int64

<class 'pandas.core.series.Series'>

Sum of each DataFrame row:

0     12

1     30

2    150

dtype: int64

<class 'pandas.core.series.Series'>

 

Applying a Python function to each element of a pandas DataFrame:

  • The example below applies math.sin() function on a DataFrame that has angle data using the method DataFrame.applymap(). For each element of the DataFrame the function math.sin() is called once. The result is returned as a new DataFrame instance that has the transformed elements.

Example:

# Example Python program to apply a function 
# that transforms the elements of a pandas DataFrame

import pandas as pd
import math

panelAngles = [(15, 20, 25),
               (30, 35, 40),
               (45, 50, 55)];

rows        = ["R1", "R2", "R3"];
columnNames = ["C1", "C2", "C3"];

dataFrame = pd.DataFrame(data=panelAngles, index = rows, columns = columnNames);
sinValues = dataFrame.applymap(math.sin);

print("DataFrame:");
print(dataFrame);

print("The function math.sine applied on the DataFrame elements:")
print(sinValues);

Output:

DataFrame:

    C1  C2  C3

R1  15  20  25

R2  30  35  40

R3  45  50  55

The function math.sine applied on the DataFrame elements:

          C1        C2        C3

R1  0.650288  0.912945 -0.132352

R2 -0.988032 -0.428183  0.745113

R3  0.850904 -0.262375 -0.999755

 


Copyright 2024 © pythontic.com