Applying pow() function on to a Pandas DataFrame instance

Overview:

  • The built-in function pow() raises a Python numeric variable to a power exponent.
  • In the similar way, the pow() method of a pandas DataFame instance,  raises its elements to a numeric power as given by the elements of another pandas DataFrame instance.

Example:

# Example Python program to raise a dataframe elements

# to the power exponent as defined by the elements of

# another dataframe

 

import pandas as pd

 

# Data 1

m1 = [(0, 1, 2),

      (3, 4, 5),

      (6, 7, 8)];

 

# Data 2

m2 = [(1, 1, 1),

      (2, 2, 2),

      (3, 3, 3)];

 

# Create pandas dataframe instances from lists of tuples

dataFrameInstance1 = pd.DataFrame(data=m1);

dataFrameInstance2 = pd.DataFrame(data=m2);

 

raised = dataFrameInstance1.pow(dataFrameInstance2);

 

# Print input data

print("Pandas DataFrame Instance1:");

print(dataFrameInstance1);

 

print("Pandas DataFrame Instance2:");

print(dataFrameInstance2);

 

# Print resultant data

print("Dataframe1 elements raised to the power of Dataframe2 elements: " );

print(raised);

 

Output:

Pandas DataFrame Instance1:

   0  1  2

0  0  1  2

1  3  4  5

2  6  7  8

Pandas DataFrame Instance2:

   0  1  2

0  1  1  1

1  2  2  2

2  3  3  3

Dataframe1 elements raised to the power of Dataframe2 elements:

     0    1    2

0    0    1    2

1    9   16   25

2  216  343  512

 

 


Copyright 2024 © pythontic.com