Python | Pandas dataframe.rpow ()

Python Methods and Functions

The Pandas dataframe.rpow() function is used to find the exponential cardinality of data and other elements element by element (binary operator rfloordiv). This function is essentially the same as for other ** dataframe but with support for replacing missing data in one of the inputs.

Syntax: DataFrame.rpow (other, axis = 'columns', level = None, fill_value = None)
Parameters:
other: Series, DataFrame, or constant
axis: For Series input, axis to match Series index on
level: Broadcast across a level, matching Index values on the passed MultiIndex leve
fill_value: Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.

Returns: result: DataFrame

Example # 1: Use rpow () to bump each row element to the corresponding value in the data frame along the column axis.

# import pandas as pd

import pandas as pd

 
# Create data frame

df = pd.DataFrame ({ "A" : [ 1 , 5 , 3 , 4 , 2 ] ,

"B" : [ 3 , 2 , 4 , 3 , 4 ], 

  "C" : [ 2 , 2 , 7 , 3 , 4 ], 

"D" : [ 4 , 3 , 6 , 12 , 7 ]},

index = [ "A1" , "A2" , "A3" , "A4" , "A5" ])

 
# Print the data frame
df

Let's create a series

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ 12 , 25 , 64 , 18 ], index = [ "A" , "B" , "C" , "D" ])

 
# Print series
sr

Let's use the dataframe.rpow () function to bump each element into sequence to the degree of the corresponding element in the data frame.

# equivalent to sr ** df

df.rpow (sr, axis = 1 )

Output:

Example # 2: Use rpow () to raise each item in the dataframe to the extent of the corresponding element in another dataframe

# import pandas as pd

import pandas as pd

  
# Create first data frame

df1 = pd.DataFrame ({ "A" : [ 1 , 5 , 3 , 4 , 2 ],

" B " : [ 3 , 2 , 4 , 3 , 4 ],

"C" : [ 2 , 2 , 7 , 3 , 4 ],

  " D " : [ 4 , 3 , 6 , 12 , < / code> 7 ]},

  index = [ "A1 " , " A2 " , "A3" , "A4" , "A5" ])

  
# Create second data frame

df2 = pd.DataFrame ({ "A" : [ 10 , 11 , 7 , 8 , 5 ],

  " B " : [ 21 , 5 , 32 , 4 , 6 ],

"C" : [ 11 , 21 , 23 , 7 , 9 ],

"D" : [ 1 , 5 , 3 , 8 , 6 ]}, 

index = [ "A1" , " A2 " , " A3 " , "A4" , "A5" ])

 
# Print first frame of data

print (df1)

 
# Print the second frame 

print (df2)

Executes df2 ** df1

# raise df2 to df1
df1.rpow (df2)

Output:





Get Solution for free from DataCamp guru