Write a program in Python to print the power of all the elements in a given series


Input − Assume, you have a series,

0    1
1    2
2    3
3    4

Output − And, the result for the power of all elements in a series is,

0    1
1    4
2    27
3    256

Solution 1

  • Define a Series.

  • Create transform method inside apply lambda power value. It is defined below, data.transform(lambda x:x**x)

  • data.transform(lambda x:x**x)

Solution 2

  • Define a Series.

  • Create an empty list

  • . Create for loop, iter all the items. Append elements to the list. It is defined below,

for i,j in data.items():
   ls.append(m.pow(j,j))
  • Finally, convert the list into Series.

Example

Let us see the following implementation to get a better understanding.

import pandas as pd
l = [1,2,3,4]
data = pd.Series(l)
print(data.transform(lambda x:x**x))

Output

0    1
1    4
2    27
3    256

Solution 3

Example

import pandas as pd
import math as m
l = [1,2,3,4]
data = pd.Series(l)
ls = []
for i,j in data.items():
   ls.append(m.pow(j,j))
result = pd.Series(ls)
print(result)

Output

0    1.0
1    4.0
2    27.0
3    256.0

Updated on: 24-Feb-2021

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements